linq to entities查询哪个有多个where子句,哪个where子句有where where condition

时间:2014-10-13 21:08:12

标签: c# sql linq linq-to-sql linq-to-entities

我正在尝试构建linq到实体查询。这就是我到目前为止所做的:

            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()

我不知道如何做IN部分,我正在粘贴我正在使用的实际sql查询。 我该如何翻译那个sql查询

  

'SELECT Line FROM BusSchedule WHERE NameOfTown = destination'

在Linq to Entities查询中?

提前致谢,Laziale

1 个答案:

答案 0 :(得分: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()