Not supported in LINQ to Entities and Entity Framework ..

I was forced to explore something interesting when i was playing around with the Entity Framework and executing the below LINQ query.

EmployeeEntities entityContext = new EmployeeEntities();
var Records = (from m in entityContext.Employees
  Select m).LastOrDefault();

My assumption was that the result would be the last record in the table .. But to my surprise , it threw an Exception

LINQ to Entities does not recognize the method ‘Employees’ Last(System.Linq.IQueryable`1)’ method, and this method cannot be translated into a store expression.

Instead , the below query worked..

EmployeeEntities entityContext = new EmployeeEntities();
var Records = (from m in entityContext.Employees
  Select m).List<Employees>().LastOrDefault();

Interestingly , here’s the reason why i got the error .

The above said functions and there are still quite a few LINQ operators that are not supported in the LINQ to Entities and the reason why other query worked with List is simply because the data from the SQL is loaded to the Generic List first and the LastOrDefault is applied directly to the List and not to the SQL .

You can find the list of functions that are not supported in .NET Framework in the MSDN site

Supported and Unsupported LINQ Methods (LINQ to Entities)

Moral of the story is “Get to know what is supported and not supported , before we start”.:)

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

4 Comments

  1. Daniel Simmons October 26, 2010 Reply
  2. Frans Bouma October 26, 2010 Reply
  3. neo November 12, 2010 Reply

Leave a Reply


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
Column Alias and Square brackets in SQL Query.
When we want to specify a alias for a column in SQL Query , we tend to use the double quotes or sing...