Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve this error in C#?

I'm getting an error when using the following code

var v1 = from P in db1.QuranWordsNews where P.Aye == perId select P;
var vv = v1.LastOrDefault(); // The error occurs here

The message:

LINQ to Entities does not recognize the method 'TashihQuran.QuranWordsNew LastOrDefaultQuranWordsNew' method, and this method cannot be translated into a store expression.

like image 303
mj-gholami Avatar asked Jul 10 '12 08:07

mj-gholami


Video Answer


2 Answers

Maybe the better answer is here :

var vv = v1.OrderByDescending(rec => rec.Id).FirstOrDefault();

Fetch all records from database to use just the last record is not good.

like image 72
Ali Foroughi Avatar answered Oct 08 '22 00:10

Ali Foroughi


I guess you're still working in IQueriable. Try instead

var vv = v1.ToList().LastOrDefault();

or, more elegant

var vv = v1.AsEnumerable().LastOrDefault();
like image 33
phipsgabler Avatar answered Oct 08 '22 01:10

phipsgabler