我有一组集合,其构建方式是“children”(possible_child) 集合中的每个对象至少应该有一个父集合 (possible_parent)。我想检测那些没有父对象的 child 。 例如,如果我找到一个没有给定国家、年份、季节和季节类型的 child ,至少应该有一个具有相同数据的父记录。
我写了下面的查询,现在我发现它是错误的。
var childrenMissingParents = (from cl in possible_child
join pr in possible_parent on new
{
p1=cl.Country,
p2=cl.Year,
p3=cl.Season,
p4=cl.SeasonType
}
equals
new
{
p1=pr.Country,
p2=pr.Year,
p3=pr.Season,
p4=pr.SeasonType
}
into opr from spr in opr.DefaultIfEmpty()
where spr == null select cr).ToList();
有人可以提出更好的主意吗?
最佳答案
var childrenMissingParents = possible_child.Where(
c => !possible_parent.Any(
p => p.Country == c.Country
&& p.Year == c.Year
&& p.Season == c.Season
&& p.SeasonType == c.SeasonType));
关于c# - 使用 LINQ 查找缺少父对象的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755954/