25 Haziran 2012 Pazartesi

error: LINQ to Entities does not recognize the method ...Last

The complete error message is as follows :


{"LINQ to Entities does not recognize the method 'Core.Sentence Last[Sentence (System.Collections.Generic.IEnumerable`1[Core.Sentence])' method, and this method cannot be translated into a store expression."}

and the causing line is:


retList = (dal.Paragraph.Include("Sentence").Where(p => p.Sentence.Last().Topic.ID == topic.ID)).ToList();

This is because Last and LastOrDefault are just not supported by the LINQ to SQL query translator.
http://forums.asp.net/t/1480557.aspx/1

The solution is ordering Sentence in descending order and use FirstOrDefault instead of Last:

 retList = (dal.Paragraph.Include("Sentence").Where(p => p.Sentence.OrderByDescending(s => s.ID).FirstOrDefault().Topic.ID == topic.ID)).ToList();


Hiç yorum yok:

Yorum Gönder