“entities.LastOrDefault()”引發了類型“System.NotSupportedException”的異常


問題:

var entities = new ShipuPlanBLO().UserList(userId, beginDate, endDate);
DateTime maxDate = entities.FirstOrDefault().PlanDate;
DateTime minDate = entities.LastOrDefault().PlanDate; //報錯

說明: 執行當前 Web 請求期間,出現未經處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。 
異常詳細信息: System.NotSupportedException: LINQ to Entities 不識別方法“AdminDB.Models.PlanList LastOrDefault[PlanList](System.Linq.IQueryable`1[AdminDB.Models.PlanList])”,因此該方法無法轉換為存儲表達式。

 

----------------------------------------------------------------------------------------------------------------------------------------

解決方法:

That's because LINQ to Entities (and databases in general) does not support all the LINQ methods (see here for details: http://msdn.microsoft.com/en-us/library/bb738550.aspx)

What you need here is to order your data in such a way that the "last" record becomes "first" and then you can use FirstOrDefault. Note that databasese usually don't have such concepts as "first" and "last", it's not like the most recently inserted record will be "last" in the table.

問題鏈接:https://social.msdn.microsoft.com/Forums/vstudio/en-US/2ccc7935-c6f5-4bf2-a4cd-0a6de9e38ed8/firstordefault-and-lastordefault?forum=wpf

 

簡單來說就是 LINQ to Entities 不支持所有的LINQ方法,LastOrDefault也是不支持的方法之一

[That's because LINQ to Entities (and databases in general) does not support all the LINQ methods ]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM