[英]T-SQL Statement with Linq2Sql
我嘗試在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();
謝謝
錯誤“序列不包含任何元素”是由於您正在調用First()而沒有任何內容的集合的結果。
您可以改為調用FirstOrDefault來避免此問題。
編輯:
我相信在您要創建的匿名類型的情況下,默認值將為null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.