Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Entry
- {
- public int Id {get; set;}
- public string Name {get; set;}
- }
- public partial class Entry2
- {
- public Entry2()
- {
- this.Enrties = new HashSet<Entry>();
- }
- public virtual ICollection<Entry> Enrties {get; set;}
- }
- // list - содержит коллекцию элементов Entry2, внутри каждого из которых коллекция Entry
- List<Entry2> list;
- 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;
- }
- }
- // для демонстрации заполним коллекции
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement