I have an object that has a list of variable lengths (entryList in the sample code) and a list of people, each of which has a list of elements. I want to return only those people who have all the elements of the incoming list.
Therefore, looking at an example, I want only 1 and 3 people to return.
People are in the database and I want to get as little data as possible, so I'm trying to figure out what the linq query should do for this? If I knew that the length of the incoming list would always be the same, I could do "... Any (..) & & ... Any (...) & &", etc., but the length will be different .
void Main() { var incomingList = new IncomingItem(); var matchItem1 = new MatchItem { ItemType = "objectId", ItemValue = "60" }; var matchItem2 = new MatchItem { ItemType = "area", ItemValue = "CU" }; incomingList.MatchList = new List<MatchItem>(); incomingList.MatchList.Add(matchItem1); incomingList.MatchList.Add(matchItem2); var people = new List<Person>(); var person1 = new Person { Id = 1 }; person1.ListOfItems = new List<Item>(); person1.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "60" }); person1.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "1" }); person1.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "30" }); person1.ListOfItems.Add(new Item { ItemType = "area", ItemValue = "CO" }); person1.ListOfItems.Add(new Item { ItemType = "area", ItemValue = "CU" }); people.Add(person1); var person2 = new Person { Id = 2 }; person2.ListOfItems = new List<Item>(); person2.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "60" }); people.Add(person2); var person3 = new Person { Id = 3 }; person3.ListOfItems = new List<Item>(); person3.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "60" }); person3.ListOfItems.Add(new Item { ItemType = "area", ItemValue = "CU" }); people.Add(person3); var person4 = new Person { Id = 4 }; person4.ListOfItems = new List<Item>(); person4.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "12" }); people.Add(person4); } public class IncomingItem { public IList<MatchItem> MatchList { get; set; } } public class MatchItem { public List<object> SomeMoreInformation { get; set; } public string ItemType { get; set; } public string ItemValue { get; set; } } public class Person { public int Id { get; set; } public IList<Item> ListOfItems { get; set; } } public class Item { public int Id { get; set; } public int PersonId { get; set; } public string ItemType { get; set; } public string ItemValue { get; set; } }
source share