简体   繁体   中英

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.

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.

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

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

or, more elegant

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM