[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2100
  • Last Modified:

Linq to Entities syntax

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."
  Source="System.Data.Entity"

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?
0
dynamk
Asked:
dynamk
  • 2
  • 2
  • 2
1 Solution
 
himanshutCommented:
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.
0
 
dynamkAuthor Commented:
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.
0
 
himanshutCommented:
I had this issue as well with different collections, so I sorted out by using Lambda Expression. Try out Lambda Expressions for a try!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoCommented:
Hi dynamk;

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

Answer:
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, http://msdn.microsoft.com/en-us/library/bb738474.aspx.

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

Answer:
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.

Fernando
0
 
dynamkAuthor Commented:
Clear and concise. Thank you.
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help.  ;=)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now