如果我有这样的类(class)
public class Test
{
public string Id {get; set;}
public string Name {get; set;}
public int Value {get; set;}
}
然后:
List<Test> values = new List<Test>(); // this contains let's say 10 items
我喜欢这样:
var grouped = values.Where(x=>x.Value > 10).GroupBy(x=>x.Name);
我的问题是如何检查 grouped == null
?或者我如何检查是否没有符合该条件的分组?
我问是因为如果我这样做:
if (grouped == null) // this is false although the linq yielded no results
{
}
最佳答案
您可以使用方法Any()
:
var anyItems = grouped.Any();
您不需要检查是否为 null,因为分组将返回一个空集合而不是 null
关于C# Linq Query group by 检查结果是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038627/