Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# 使用LINQ查找缺少父对象的子对象_C#_.net_Linq - Fatal编程技术网

C# 使用LINQ查找缺少父对象的子对象

C# 使用LINQ查找缺少父对象的子对象,c#,.net,linq,C#,.net,Linq,我有一组集合,以这样的方式构建:Children可能的\u子集合中的每个对象都应该至少有一个父集合父集合可能的\u父集合。我想检测那些没有父对象的子对象。 举个例子,如果我发现一个孩子的国家、年份、季节和季节类型没有,那么至少应该有一个具有相同数据的家长记录 我写了下面的查询,我现在看到它是错误的 var childrenMissingParents = (from cl in possible_child join pr in possible_parent on new { p1=c

我有一组集合,以这样的方式构建:Children可能的\u子集合中的每个对象都应该至少有一个父集合父集合可能的\u父集合。我想检测那些没有父对象的子对象。 举个例子,如果我发现一个孩子的国家、年份、季节和季节类型没有,那么至少应该有一个具有相同数据的家长记录

我写了下面的查询,我现在看到它是错误的

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 orphanedItems = possible_child
    .Where(item => !possible_parent.Any(p =>
        (p.Year == item.Year) &&
        (p.Country == item.Country) &&
        (p.Season== item.Season) &&
        (p.SeasonType == item.SeasonType)));

如果我理解正确,以下内容符合您的要求:

var orphanedItems = possible_child
    .Where(item => !possible_parent.Any(p =>
        (p.Year == item.Year) &&
        (p.Country == item.Country) &&
        (p.Season== item.Season) &&
        (p.SeasonType == item.SeasonType)));

你可以在任何地方和任何地方实现你的目标

var childrenWithoutParent = possible_child.Where(child => !possible_parent.Any(p =>
                                                                   (p.Year == child.Year) &&
                                                                   (p.Country == child.Country) &&
                                                                   (p.Season == child.Season) &&
                                                                   (p.SeasonType == child.SeasonType)));
但是,您可以进一步提高代码的可读性,您的子级中可以有一个方法与父级进行比较:

public class Child
    {
        ....

        public bool IsEqualsTo(Parent parent)
        {
            return (this.Year == parent.Year) &&
                   (this.Country == parent.Country) &&
                   (this.Season == parent.Season) &&
                   (this.SeasonType == parent.SeasonType)));
        }
    }
这可以提高查询的可读性

  var childrenWithoutParent = possible_child
                                    .Where(child => !possible_parent.Any(p => child.IsEqualsTo(p));

你可以在任何地方和任何地方实现你的目标

var childrenWithoutParent = possible_child.Where(child => !possible_parent.Any(p =>
                                                                   (p.Year == child.Year) &&
                                                                   (p.Country == child.Country) &&
                                                                   (p.Season == child.Season) &&
                                                                   (p.SeasonType == child.SeasonType)));
但是,您可以进一步提高代码的可读性,您的子级中可以有一个方法与父级进行比较:

public class Child
    {
        ....

        public bool IsEqualsTo(Parent parent)
        {
            return (this.Year == parent.Year) &&
                   (this.Country == parent.Country) &&
                   (this.Season == parent.Season) &&
                   (this.SeasonType == parent.SeasonType)));
        }
    }
这可以提高查询的可读性

  var childrenWithoutParent = possible_child
                                    .Where(child => !possible_parent.Any(p => child.IsEqualsTo(p));

两个答案都解决了我的问题,万分感谢你们。两个答案都解决了我的问题,万分感谢你们。正确,我只选择了一个有效的解决方案。我也可以选择这个。许多thnakscorrect和我只是选择了一个可行的解决方案。我也可以选择这个。多刺