1

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?

Jim Wooley
  • 10,169
  • 1
  • 25
  • 43
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • what about using the `.Contains()` Method it's equivalent to IN Clause – MethodMan Oct 13 '14 at 21:14
  • @DJKRAZE can you give me a sample pls, thanks – Laziale Oct 13 '14 at 21:15
  • here is one but I would suggest doing a google search on Linq examples and reading up on the many different things you can do with `Linq` as well :) http://stackoverflow.com/questions/959752/where-in-clause-in-linq – MethodMan Oct 13 '14 at 21:16

1 Answers1

0
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()
Raein Hashemi
  • 3,346
  • 4
  • 22
  • 33