"Entities. LastOrDefault ()" causes an exception of the type "System. NotSupportedException". htmlentities
Problem:
Var entities = new ShipuPlanBLO (). userList (userId, beginDate, endDate); DateTime maxDate = entities. firstOrDefault (). planDate; DateTime minDate = entities. lastOrDefault (). planDate; // Error
Note:An unhandled exception occurred during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.
Exception details:System. notSupportedException: The method "AdminDB. models. planList LastOrDefault [PlanList] (System. linq. IQueryable '1 [AdminDB. models. planList]) ", so this method cannot be converted to a storage expression.
Certificate ----------------------------------------------------------------------------------------------------------------------------------------
Solution:
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.
Question: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2ccc7935-c6f5-4bf2-a4cd-0a6de9e38ed8/firstordefault-and-lastordefault? Forum = wpf
To put it simply, LINQ to Entities does not support all LINQ methods, and LastOrDefault is also one of the unsupported methods.
[That's because LINQ to Entities (and databases in general) does not support all the LINQ methods]