LINQ 获取最接近的值?
我有一个列表,MyStuff 有一个 Float 类型的属性。
有些对象的属性值为 10、20、22、30。
我需要编写一个查询来查找最接近 21 的对象,在本例中它将找到 20 和 22 对象。然后我需要编写一个发现对象接近 21 而不会超过的对象,并且它将返回值为 20 的对象
。我不知道从哪里/如何开始这个。帮助?
谢谢。
更新 - 哇这里有这么多很棒的回复。谢谢!我不知道该遵循哪一个,所以我会尝试所有的。可能使这变得更(或更少)有趣的是,相同的查询必须适用于 LINQ-to-SQL 实体,因此从 MS Linq 论坛获得的答案可能效果最好?不知道。
I have a List, MyStuff has a property of Type Float.
There are objects with property values of 10,20,22,30.
I need to write a query that finds the objects closest to 21, in this case it would find the 20 and 22 object. Then I need to write one that finds the object closes to 21 without going over, and it would return the object with a value of 20.
I have no idea where/how to begin with this one. Help?
Thanks.
Update - wow there are so many awesome responses here. Thanks! I don't know which one to follow so I will try them all. One thing that might make this more (or less) interesting is that the same query will have to apply to LINQ-to-SQL entities, so possibly the answer harvested from the MS Linq forums will work the best? Don't know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试按数字与 21 之间的差的绝对值对它们进行排序,然后取第一项:
或者根据 @Yuriy Faktorovich 的评论缩短它:
Try sorting them by the absolute value of the difference between the number and 21 and then take the first item:
Or shorten it according to @Yuriy Faktorovich's comment:
这是在线性时间内满足第二个查询的解决方案:(
澄清后从“上方”编辑为“下方”)
至于第一个查询,最简单的是使用
MoreLinq
的MinBy
扩展:也可以在标准 LINQ 中以线性时间完成此操作,但需要 2源的传递:
如果效率不是问题,您可以对序列进行排序并选择
O(n * log n)
中的第一个值,正如其他人发布的那样。Here's a solution that satisfies the second query in linear time:
(Edited from 'above' to 'below' after clarification)
As for the first query, it would be easiest to use
MoreLinq
'sMinBy
extension:It's also possible to do it in standard LINQ in linear time, but with 2 passes of the source:
If efficiency is not an issue, you could sort the sequence and pick the first value in
O(n * log n)
as others have posted.基于这篇文章 在 Microsoft Linq 论坛上:
Based on this post at the Microsoft Linq forums:
或者
,这里有一个扩展方法:
用法:
OR
and here comes an extension method:
Usage:
从.net 6开始可以使用 MinBy为了以与投票最多的帖子类似的方式实现结果。它现在是标准 Linq 的一部分。
用法:
Since .net 6 MinBy can be used in order to achieve the result in similar manner as in the most upvoted post. It's now a part of standard Linq.
usage: