返回具有最大值的所有行

发布于 2024-09-18 12:46:01 字数 486 浏览 2 评论 0原文

我正在使用实体框架来连接数据库。

我有一个表(我们称之为“文件”),其中有几个字段:

ID、版本、XYZ Primarky 密钥基于 ID 和版本。 所以我可以有几行具有相同的 ID 但不同的版本(反之亦然)。

问题是:

我如何使用 LAMBDA 表达式询问我的实体框架,返回“文件”的所有最新版本。

例子: 数据:

 ID;Version;Other
 1;1;YX
 1;2;YZ
 2;1;AH
 2;2;BH
 2;5;CA
 1;3;AAA

结果:

 1;3;AAA
 2;5;CA

谢谢!

!!目标是数据库不需要返回所有行,并且仅被调用一次,因此忘记像 GetAllRows 这样的解决方案并读取整个集合并仅保存最新的,或者获取所有可能的 ID 的列表并获取最后一个另一个请求中的 foreach 版本。谢谢!

I'm using entity framework to connect the database.

I've a table(Let's call it "File") that haves several fields:

ID, Version, XYZ
Primarky key is based on ID AND Version.
so I can have several line with the same ID but different version(and inversly).

The question is:

How can I, with a LAMBDA expression, ask my Entity Framework, to return me all last version of a "file".

Example:
Datas:

 ID;Version;Other
 1;1;YX
 1;2;YZ
 2;1;AH
 2;2;BH
 2;5;CA
 1;3;AAA

Result:

 1;3;AAA
 2;5;CA

Thank you!

!! The goal is that the database doesn't need to return all rows, and is called only one time, so forget solution like GetAllRows and read the whole collection and save only the latest, or get a list of all possible ID and get the last version foreach in another request. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-09-25 12:46:01

为此,您可以使用以下 LinqToEntites 查询:

var result = from f in myEntities.Files
             group f by f.ID into g
             select g.OrderByDescending(f => f.Version).FirstOrDefault();

使用 First 而不是 FirstOrDefault 可能更有意义,但随后您会收到 UnsupportedException

方法“First”只能用作最终查询操作。考虑在此实例中使用“FirstOrDefault”方法

You can use the following LinqToEntites query for this:

var result = from f in myEntities.Files
             group f by f.ID into g
             select g.OrderByDescending(f => f.Version).FirstOrDefault();

It would perhaps make more sense to use First instead of FirstOrDefault but then you get an UnsupportedException:

The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文