Roman
Roman

Reputation: 4513

How use LINQ to find entities where a list of sub entities contains another list

I have a complex object which contains 2 lists of more complex objects.

The first object is as follows:

public object1 {
        public string Name { get; set; }
        public virtual ICollection<object2> objects2 { get; set; }
        public virtual ICollection<object3> objects3 { get; set; }
}

Where as object2 and object3 are the same, having an ID and Name field. Such as:

public object {
   public int ID{ get; set; }
   public string Name { get; set; }
}

I would like to use a list / array of IDs (int[] ids) to search a list of 'object1's so that the field 'objects2' contains all the array o2_ids and 'objects3' all the array o3_ids (each one is int[]).

Upvotes: 0

Views: 278

Answers (1)

Anders Abel
Anders Abel

Reputation: 69270

from o1 in obj1List
where o2_ids.All(o2id => o1.objects2.Any(o2 => o2.ID == o2id))
&& o3_ids.All(o3id => o1.objects3.Any(o3 => o3.ID == o3id))
select o1

Upvotes: 2

Related Questions