程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> “entities.LastOrDefault()”引發了類型“System.NotSupportedException”的異常,htmlentities

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

編輯:C#入門知識

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


問題:

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 ]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved