gpt4 book ai didi

linq - 通过 linq 选择最后一条记录失败,错误代码为 "method not recognised"

转载 作者:行者123 更新时间:2023-12-02 10:52:31 27 4
gpt4 key购买 nike

我有以下查询来选择数据库中的最后一条记录

using (Entities ent = new Entities())
{
Loaction last = (from x in ent.Locations select x).Last();
Location add = new Location();
add.id = last.id+1;
//some more stuff
}

通过 ext.net 上的“Direct event”调用包含这些行的方法时,返回以下错误:

LINQ to Entities does not recognize the method 'Prototype.DataAccess.Location Last[Location]
(System.Linq.IQueryable`1[Prototype.DataAccess.Location])' method,
and this method cannot be translated into a store expression.

表结构如下:

int ident IDENTITY NOT NULL,
int id NOT NULL,
varchar(50) name NULL,
//some other varchar fields

最佳答案

Linq to Entities 不支持 Last。按降序排序(通常按 ID 或某个日期列)并选择第一个。像这样的东西:

Location last = (from l in ent.Locations
orderby l.ID descending
select l).First();

或者

Location last = ent.Locations.OrderByDescending(l => l.ID).First();

请参阅 MSDN 文章 Supported and Unsupported LINQ Methods (LINQ to Entities)供引用。

关于linq - 通过 linq 选择最后一条记录失败,错误代码为 "method not recognised",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15219469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com