"Entities. LastOrDefault ()" causes an exception of the type "System. NotSupportedException". htmlentities

Source: Internet
Author: User

"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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.