如何使用Linq to Entity获得最大ID? [英] How do I get the max ID with Linq to Entity?

查看:56
本文介绍了如何使用Linq to Entity获得最大ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表User,表中有一个标识列UserID,现在正确的Linq to Entity代码行是什么,这将使我得到最大的UserID?

I have a table User which has an identity column UserID, now what is the correct Linq to Entity line of code that would return me the max UserID?

我尝试过:

using (MyDBEntities db = new MyDBEntities())
{
    var User = db.Users.Last();
    // or
    var User = db.Users.Max();

    return user.UserID;
}

但似乎不支持LastMax.

有什么想法吗?

推荐答案

做到这一点

db.Users.OrderByDescending(u => u.UserId).FirstOrDefault();

这篇关于如何使用Linq to Entity获得最大ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆