18

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.

2

2 Answers 2

45

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.

1
  • 1
    Agree with your statement, but in OP's case, doesn't v1 already contain "all records" from database?
    – gitsitgo
    Apr 24, 2014 at 16:36
35

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

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

or, more elegant

var vv = v1.AsEnumerable().LastOrDefault();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.