Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从.Any()查询中检索匹配项_C#_Linq_Linq To Objects - Fatal编程技术网

C# 从.Any()查询中检索匹配项

C# 从.Any()查询中检索匹配项,c#,linq,linq-to-objects,C#,Linq,Linq To Objects,因此,我有以下代码: 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 >

因此,我有以下代码:

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()查询匹配的单个项。如何仅获取匹配的项目?

您不能直接使用
Any()
函数(它只返回
bool
),但是
.Where()
函数将返回一个过滤后的
IEnumerable
,该函数也具有
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

您不能直接使用
Any()
函数(它只返回一个
bool
),但是
.Where()
函数将返回一个过滤后的
IEnumerable
,该函数也有
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

用Where代替any怎么样用Where代替any怎么样