I have my Microsoft SQL 2008 Database Entity with one Table containing 6 Columns where the first column is simply my primarykey which is a number which gets increased each step.
I wrote a method which returns the next primary key (an integer)
private int NextPrimaryKey()
{
int NextPrimaryKey;
using (MitarbeiterlisteEntities entities = new MitarbeiterlisteEntities())
{
NextPrimaryKey = entities.Mitarbeiterliste.LastOrDefault().primaerschluessel;
NextPrimaryKey++;
}
return NextPrimaryKey;
}
The Problem is, that I always get an error saying:
LINQ to 开发者_开发百科Entities does not recognize the method 'IntraNET_Prototype.Mitarbeiterliste LastOrDefaultMitarbeiterliste' method, and this method cannot be translated into a store expression.
What's wrong? I simply just want to access the first value(column) in the last element which is primaerschluessel. Any ideas?
Last
and LastOrDefault
is not supported in Linq-to-Entities.
You must use:
NextPrimaryKey = entities.Mitargeiterliste
.Select(e => e.primaerschluessel)
.OrderByDescending(p => p)
.FirstOrDefault();
=> you will first order entities in descending order and than select the first one.
精彩评论