作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
检查 IEnumerable 集合是否多于或少于 X 个满足谓词的元素的最佳方法是什么?
我目前正在使用 .Count(lambda) <= limit
但这会使程序不必要地遍历整个集合。
最佳答案
您可以使用此表达式:.Skip(limit).Any()
等效于 Count() > limit
。但是,如果您的列表是 ICollection
,则 Count()
更为可取。
谓词版本:
public static bool MoreThan<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate, int limit)
{
int i = 0;
foreach (var item in source)
{
if (predicate(item))
{
i++;
if (i > limit)
{
return true;
}
}
}
return false;
}
关于c# - LINQ MoreThan(predicate, limit) 扩展而不是 Count(predicate) > limit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6828446/
我想使用从 map 上获取的边界框按地理点查询文档集合。我有界限: 北:50.730 东:-1.861 南方:50.700 西:-1.893 这是我的实际文档结构(基于我期望它返回的值): docum
检查 IEnumerable 集合是否多于或少于 X 个满足谓词的元素的最佳方法是什么? 我目前正在使用 .Count(lambda) limit。但是,如果您的列表是 ICollection,则
我是一名优秀的程序员,十分优秀!