Linq2Sql的T-SQL语句

时间:2012-07-13 20:48:57

标签: c# linq-to-sql

我尝试在Linq2SQL中转换以下SQL Select语句:

SELECT stoptimes.stopid,
       trips.tripid,
       stoptimes.sequence
FROM   trips
       INNER JOIN stoptimes
               ON stoptimes.tripid = trips.tripid
WHERE  ( trips.routeid = '3' )
       AND ( trips.endplace = 'END001' )
ORDER  BY stoptimes.sequence DESC 

它运行良好但是对于linq2sql,我得到以下语句的异常:

var first = (from tableTrip in db.Trips
             join tableStopTimes in db.StopTimes on tableTrip.TripId equals tableStopTimes.TripId
             where tableTrip.RouteId == 3 && tableTrip.EndPlace == "TAEND"
             select new
             {
                 tableStopTimes.StopId,
                 tableStopTimes.Radius,
                 tableStopTimes.PlaceName,
                 tableStopTimes.Place,
                 tableStopTimes.Sequence
             }).OrderByDescending(X => X.Sequence).First();

由于

1 个答案:

答案 0 :(得分:0)

错误“Sequence contains no elements”是您在调用First()时没有任何内容的集合的结果。

您可以拨打FirstOrDefault来避免此问题。

编辑:

我相信您正在创建的匿名类型的情况,默认值为null。