class Entry { public int Id {get; set;} public string Name {get; set;} } class Entry2 { public Entry2() { this.Enrties = new HashSet<Entry>(); } public virtual ICollection<Entry> Enrties {get; set;} } List<Entry2> list; // содержит N количество элементов Entry2 

How to get Entry elements included in each Entry2? That is, if it is not contained in at least one Entry2, it does not fall into the final collection.

    1 answer 1

    If you decide to head it, you can:

     List<Entry2> list = new List<Entry2> { new Entry2 { Enrties = new List<Entry> { new Entry(1, "") } }, new Entry2 { Enrties = new List<Entry> { new Entry(1, "") } }, new Entry2 { Enrties = new List<Entry> { new Entry(1, "") } }, new Entry2 { Enrties = new List<Entry> { new Entry(1, "2"), new Entry(2, "") } } }; List<Entry> e2 = new List<Entry>(); foreach (var entry2 in list) { foreach (var entity in entry2.Enrties) { if (list.TrueForAll(x => x.Enrties.Contains(entity))) { e2.Add(entity); } } } foreach (var entry2 in e2) { Console.WriteLine(entry2); } 

    This solution will work if you override Equals and GetHashCode in your classes.