开发者

C# LINQ DatabaseEntitie Query to first Value in Last Element results in Error

开发者 https://www.devze.com 2023-04-12 16:54 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号