Title: Find children objects missing parents objects using LINQ
Tags: c#;.net;linq
Question: I have a set of collection built in such a way that every object in the "children"(possible_child) collection should at least have a parent the parent collection (possible_parent). I would like to detect those children with no parent object.
As an example, if i find a child with a given country, year, season and season type without there should at least be a parent record with the same data.

I wrote the following query which i now see is wrong. 

```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();
```

Can someone suggest a better idea?


Here is the accepted answer: ```var childrenMissingParents = possible_child.Where(
    c =&gt; !possible_parent.Any(
        p =&gt; p.Country == c.Country
     &amp;&amp; p.Year == c.Year
     &amp;&amp; p.Season == c.Season
     &amp;&amp; p.SeasonType == c.SeasonType));
```

Comment for this answer: Both answers solve my problem thousand thanks to both of you.

Here is another answer: If I understood correctly, the following does what you want:

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

Comment for this answer: correct and i just chose one of the working solutions. I could have chosen this one as well. Many thnaks

Here is another answer: You could use Where and Any to achieve your goal.

```var childrenWithoutParent = possible_child.Where(child =&gt; !possible_parent.Any(p =&gt;
                                                                   (p.Year == child.Year) &amp;&amp;
                                                                   (p.Country == child.Country) &amp;&amp;
                                                                   (p.Season == child.Season) &amp;&amp;
                                                                   (p.SeasonType == child.SeasonType)));
```

However, you can improve even more the read of your code, you could have a method in your child to compare to the parent:

```public class Child
    {
        ....

        public bool IsEqualsTo(Parent parent)
        {
            return (this.Year == parent.Year) &amp;&amp;
                   (this.Country == parent.Country) &amp;&amp;
                   (this.Season == parent.Season) &amp;&amp;
                   (this.SeasonType == parent.SeasonType)));
        }
    }
```

This could improve the readability of your query.

```  var childrenWithoutParent = possible_child
                                    .Where(child =&gt; !possible_parent.Any(p =&gt; child.IsEqualsTo(p));
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:11/17/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><.net><linq>', 'Title': 'Find children objects missing parents objects using LINQ', 'ViewCount': 168, 'id': 33755954, 'user_AboutMe': None, 'user_AccountId': 950048.0, 'user_DisplayName': 'Meelfan Bmfp', 'user_Reputation': 605.0, 'user_Views': 89.0, 'user_id': 976374.0}}