我需要检查所有定义是否包含一些特定数据。除了GroupBy返回空集合的情况外,它工作正常。Linq全部为空收集
var exist = dbContext.Definitions
.Where(x => propertyTypeIds.Contains(x.PropertyTypeId) && x.CountryId == countryId)
.GroupBy(x => x.PropertyTypeId)
.All(...some condition...);
如何重写这个所有将在空集合上返回false?
更新: 这是一个LINQ to SQL,我想在单个调用中执行此操作。
UPDATE2: 我想这样的作品:
var exist = dbContext.Definitions
.Where(x => propertyTypeIds.Contains(x.PropertyTypeId) && x.CountryId == countryId)
.GroupBy(x => x.PropertyTypeId)
.Count(x => x
.All(...some condition...)) == propertyTypeIds.Count;
这是LINQ的对象或其他东西?答案可能完全不同。 –
尝试使用AllOrDefault – elloco999
'All'不会在空集合上返回false。看到这里:http://stackoverflow.com/questions/7884888/why-does-enumerable-all-return-true-for-an-empty-sequence – sr28