Linq to Entities syntax

Posted on 2009-12-16
Last Modified: 2013-11-11
Can someone explain to me why the following statement:
   var record = _efEntities.Records.LastOrDefault();
Throws this error:
    System.NotSupportedException was unhandled
  Message="LINQ to Entities does not recognize the method 'EFSchedulerDemo.Resource Last[Resource](System.Linq.IQueryable`1[EFSchedulerDemo.Resource])' method, and this method cannot be translated into a store expression."

But I am able to use:
    var record = _efEntities.Records.ToList().LastOrDefault();

How can I get the first or last record without having to call ToList() every time if possible?
Question by:dynamk
    LVL 10

    Expert Comment

    Use a foreach to traverse the List of records and take the value where i=0, i<List.count-1

    Hope that should give you the Last and first record out of the List.

    Author Comment

    What I am trying to find out is how to get the entity object Without using the List. The List already works fine but I don't understand why I cannot just use var record =_efEntities.Records.LastOrDefault() without getting an error.
    LVL 10

    Expert Comment

    I had this issue as well with different collections, so I sorted out by using Lambda Expression. Try out Lambda Expressions for a try!
    LVL 62

    Accepted Solution

    Hi dynamk;

    In answer to your question, "Can someone explain to me why the following statement:
       var record = _efEntities.Records.LastOrDefault();
       Throws this error:"

    The reason why the above statement throws an error is that not all Linq operators are supported in LINQ to Entities queries, please see this link documenting this,

    To the second half of the question, "But I am able to use:
        var record = _efEntities.Records.ToList().LastOrDefault();"

    This statement, var record = _efEntities.Records.LastOrDefault();, instructs the Entity Framework to construct a T-SQL query using the unsupported operator LastOrDefault(). While this statement,  var record = _efEntities.Records.ToList().LastOrDefault();, uses _efEntities.Records to build the T-SQL then the returned result is exported to a generic List and then the LastOrDefault() is applied to the List object. Therefore the LastOrDefault() is not used in the T-SQL query on the results of that query.

    I hope that answers your question.


    Author Closing Comment

    Clear and concise. Thank you.
    LVL 62

    Expert Comment

    by:Fernando Soto
    Not a problem, glad I was able to help.  ;=)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now