You need to add a bit to your Entry class with the IEquatable interface:
public partial class Entry : IEquatable<Entry> { public bool Equals(Entry other) { if (Object.ReferenceEquals(other, null)) return false; if (Object.ReferenceEquals(this, other)) return true; return Id.Equals(other.Id) && Name.Equals(other.Name); } public override int GetHashCode() { int hashProductName = Name == null ? 0 : Name.GetHashCode(); int hashProductCode = Id.GetHashCode(); return hashProductName ^ hashProductCode; } }
After that, here is the code:
// Π΄Π»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ Π·Π°ΠΏΠΎΠ»Π½ΠΈΠΌ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ List<Entry2> lst = new List<Entry2>() { new Entry2() { Enrties = new List<Entry>() { new Entry() {Id = 1, Name = "1"}, new Entry() {Id = 2, Name = "2"}, new Entry() {Id = 3, Name = "3"} } }, new Entry2() { Enrties = new List<Entry>() { new Entry() {Id = 3, Name = "3"}, new Entry() {Id = 4, Name = "4"}, new Entry() {Id = 5, Name = "5"} } }, new Entry2() { Enrties = new List<Entry>() { new Entry() {Id = 5, Name = "5"}, new Entry() {Id = 6, Name = "6"}, new Entry() {Id = 7, Name = "7"} } }, }; var distinct = lst.SelectMany(x => x.Enrties).Distinct().ToList(); // 1,2,3,4,5,6,7
As a result, only unique variables will be in the distinct variable.
Some explanations about SelectMany :
SelectMany - projects each element of a sequence into an IEnumerable<T> , merges the resulting sequences into one and calls the result selector function for each element of this sequence.
A few explanations about Distinct :
Distinct - returns distinct elements of a sequence, using the comparator for equality by default to compare values.
IEquatable inherited and Equals and GetHashCode methods were implemented so that Distinct could be compared according to our rules , and not by default.
@ΠΠ°ΡΠ° Entry2@.Enrties.Distinct().ToList()? Distinct You can also useWhere("ΠΠ°ΡΠ΅ ΡΡΠ»ΠΎΠ²ΠΈΠ΅")Where Although, judging by the edit, you need SelectMany - Denis Bubnov