Skip to content
Advertisement

linq to entities query which has multiple where clause and one where clause with where in condition

I’m trying to build a linq to entities query. This is what I have so far:

            from x in db.BusSchedule
            join y in db.BusSchedule on x.ID equals y.ID - 1
            where Convert.ToInt32(y.StopOrder) >= Convert.ToInt32(x.StopOrder) 
            && x.NameOfTown == arrival
            && x.Line in (SELECT Line FROM BusSchedule WHERE NameOfTown = destination) 
            orderby x.DepartureTime ascending
            select x).Distinct()

I’m not sure how to do the IN part, and I’m pasting the actual sql query I’m using. How can I translate this SQL query

‘SELECT Line FROM BusSchedule WHERE NameOfTown = destination’

in Linq to Entities query?

Advertisement

Answer

var dest = (from b in db.BusSchedule
           where b.NameOfTown = destination
           select b.Line).ToList();

var output = (from x in db.BusSchedule.Where(b => b.NameOfTown == arrival)
             join y in db.BusSchedule on x.ID equals y.ID - 1
             where Convert.ToInt32(y.StopOrder) >= Convert.ToInt32(x.StopOrder) 
                   && dest.Contains(x.Line)
             orderby x.DepartureTime ascending
             select x)
             .Distinct()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement