0

This is my SQL query:

select 
    m.Name, s.Time, t.TheaterNumber
from   
    Movies m
join 
    MovieSeanceTheaters mst on mst.MovieId = m.MovieID
join 
    Theaters t on t.ID = mst.TheaterId
join 
    Seances s on mst.SeanceId = s.ID

This is my attempt at a Linq query:

var result = (from m in _context.Movies
              join mst in _context.MovieSeanceTheaters on m.ID equals mst.MovieId
              join t in _context.Theaters on mst.TheaterId equals t.ID
              join s in _context.Seances on mst.TheaterId equals s.ID
              select new { Film = m.Name, Salon = t.Name, Seans = s.Time }
             ).ToList();

I made this attempt, but I want to make with lambda for instance:

var result = movieManager.GetAll().Where(x => x.MovieSeanceTheaters).... 

I couldn't do that.

1
  • What could you not do? Also, your attempt has a different starting point. And then, you shouldn't use join at all. Use navigation properties. Commented Nov 16, 2019 at 13:30

1 Answer 1

1

If I understand you correctly, you want to rewrite your query from query syntax to method syntax?

Here we are!

var result = _context.Movies
    .Join(_context.MovieSeanceTheaters,
        m => m.MovieID,
        mst => mst.MovieID,
        (m, mst) => new
        {
            m = m,
            mst = mst
        })
    .Join(_context.Theaters,
        temp0 => temp0.mst.TheaterID,
        t => t.ID,
        (temp0, t) =>
            new
            {
                temp0 = temp0,
                t = t
            })
    .Join(_context.Seances,
        temp1 => temp1.temp0.mst.TheaterID,
        s => s.ID,
        (temp1, s) =>
            new
            {
                Film = temp1.temp0.m.Name,
                Salon = temp1.t.TheaterNumber,
                Seans = s.Time
            });

Looks ugly, doesn't it?
Most often, the method syntax is more compact and convenient. But in this case, leave it as is.

Sign up to request clarification or add additional context in comments.

2 Comments

@clleker - yes, EF join for us, if navigation properties are available. Do you have any?
I use codefirst my project so I want to make lambda ,I don't know which table to start with

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.