Linq to dataset 根据列的最大值选择行
我有一个数据集表,我想按列MOID
对其进行分组,然后在该组中我想选择具有列radi
最大值的行。
谁能告诉我如何通过 LINQ to dataset 来做到这一点?
I have a dataset table, I want to group it by column MOID
, and then within this group I want to select the row which has max value of column radi
.
Can anybody show me how to do it via LINQ to dataset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管 Barry 发布的解决方案应该可以工作(需要进行一些修复),但它并不是最优的:您不需要对集合进行排序来查找具有字段最大值的项目。我编写了一个
WithMax
扩展方法,它返回具有指定函数最大值的项目:它仅迭代集合一次,这比仅对它进行排序以获取第一项要快得多。
然后您可以按如下方式使用它
Although the solution posted by Barry should work (with a few fixes), it is sub-optimal : you don't need to sort a collection to find the item with the maximum value of a field. I wrote a
WithMax
extension method, which returns the item with the maximum value of the specified function :It iterates the collection only once, which is much faster than sorting it just to get the first item.
You can then use it as follows
这是未经测试的,但我认为这样的东西应该有效:
This is untested but I think something like this should work:
这适用于一个查询!
this works with one query!