The second time you call for .SelectMany(x => x.Contacts)
, you are currently working with a collection of ContactDetails
. It is doubtful that you would be able to use SelectMany
on it. You would need to use Select
instead.
SelectMany
is used when you want to select multiple collections of items and put them into one IEnumerable
. Select
is used on individual fields. Since you are working with objects of type ContactDetail
(which I assume can only have one contact), you would need use Select
EDIT: Here is what you're doing in a nutshell, step by step:
groups_to_querry.SelectMany(x => x.Contacts)
: From all the groups that I want to query select all of their many contacts. Each group has many contacts, so put them all into a single IEnumerable
collection of type Contact
.Where(x => x.ID == Guid.Empty)
: ...but only those Contacts with an empty ID
.SelectMany(p => p.ContactDetails)
: Then select all of those Contacts' many ContactDetails. Each Contact has many ContactDetails, so put them all into a single IEnumerable
collection of type ContactDetail
.Where(x => x.ID == Guid.Empty)
: ...but only those ContactDetails with an empty ID
.SelectMany(x => x.Contacts);
: Now select each of the ContactDetails' many Contacts. However, since the compiler knows that there is a one-to-many relationship between Contacts and ContactDetails (and not the other way around) that statement is not possible, and thus shows a compile error