LINQ,如何使用"where"条件获取Max ID

Vik*_*ali 0 c# linq

我已经搜索了堆栈溢出,但只查找了最大ID的示例,我想查找某些特定条件的最大ID.像这样的东西

var nRow = from p in ctx.FormControls
.          where p.FormObjectsId == FormID
.          select Max(p.Id);
Run Code Online (Sandbox Code Playgroud)

怎么做 ?

Hab*_*bib 5

你可以这样做:

var max = ctx.FormControls.Where(r => r.FormObjectsID == FormID)
                            .Max(r => r.Id);
Run Code Online (Sandbox Code Playgroud)

使用您的查询表达式,您可以:

var nRow =  (from p in ctx.FormControls
            where p.FormObjectsId == FormID
            select p.Id).Max();
Run Code Online (Sandbox Code Playgroud)