When I use the query
var NewMatchs = (from x in entity.matches select x).LastOrDefault();
I get error like this
LINQ to Entities does not recognize the method 'SocialSports.Models.match LastOrDefaultmatch' method, and this method cannot be translated into a store expression.
What's wrong in my code ???
Thanks...
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.
LINQ applies the principles of object-oriented programming to relational data. It provides a unified programming model for querying data from different types of data sources, and extends data capabilities directly into the C# and Visual Basic languages. For more information, see Language-Integrated Query (LINQ).
We can see right away that LINQ is a lot slower than raw SQL, but compiled LINQ is a bit faster. Note that results are in microseconds; real-world queries may take tens or even hundreds of milliseconds, so LINQ overhead will be hardly noticeable.
Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.
LastOrDefault
is not supported in Linq to Entities. You can achieve the same using the following:
var lastmatch = (from x in entity.matches select x)
// Assuming you have some kind of timestamp
.OrderByDescending(s => s.Date)
.FirstOrDefault();
You can't use LastOrDefault to query EF entities, as it cannot be translated to T-SQL.
Not all the LINQ methods are supported by Linq to Entities.
List of ALL supported / not supported methods:
http://msdn.microsoft.com/en-us/library/bb738550.aspx
You could try
var NewMatchs = (from x in entity.matches select x).ToList().LastOrDefault();
but this will load ALL matches from the db and perform Linq to objects.
Or try sorting and call FirstOrDefault.
See The query operator 'LastOrDefault' is not supported
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With