所以我有以下代码:
var itemsGrouped = this.Errors.GroupBy(x => x.UniqueName).AsEnumerable();
var hasErrors = !itemsGrouped.Any((f) =>
{
var errorCount = f.ToArray()
.Where(x => x.ErrorCount.HasValue)
.Count(x => x.ErrorCount.Value > 0);
return errorCount > 2;
});
现在我想检索与.Any()查询匹配的单个项目。我如何才能获得匹配的项目?
答案 0 :(得分:5)
您无法直接使用Any()
函数(它只返回bool
),但.Where()
函数将返回已过滤的IEnumerable<T>
Any()
功能。
类似于:
var itemsGrouped = this.Errors.GroupBy(x => x.UniqueName).AsEnumerable();
var invalidItems = itemsGrouped.Where((f) =>
{
var errorCount = f.ToArray()
.Where(x => x.ErrorCount.HasValue)
.Count(x => x.ErrorCount.Value > 0);
return errorCount > 2;
});
var hasErrors = !invalidItems.Any();
//Do stuff with invalidItems