Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. public partial class Entry
  2. {
  3. public int Id {get; set;}
  4. public string Name {get; set;}
  5. }
  6.  
  7. public partial class Entry2
  8. {
  9. public Entry2()
  10. {
  11. this.Enrties = new HashSet<Entry>();
  12. }
  13. public virtual ICollection<Entry> Enrties {get; set;}
  14. }
  15.  
  16. // list - содержит коллекцию элементов Entry2, внутри каждого из которых коллекция Entry
  17. List<Entry2> list;
  18.  
  19. public partial class Entry : IEquatable<Entry>
  20. {
  21. public bool Equals(Entry other)
  22. {
  23. if (Object.ReferenceEquals(other, null))
  24. return false;
  25.  
  26. if (Object.ReferenceEquals(this, other))
  27. return true;
  28.  
  29. return Id.Equals(other.Id) && Name.Equals(other.Name);
  30. }
  31.  
  32. public override int GetHashCode()
  33. {
  34. int hashProductName = Name == null ? 0 : Name.GetHashCode();
  35. int hashProductCode = Id.GetHashCode();
  36. return hashProductName ^ hashProductCode;
  37. }
  38. }
  39.  
  40. // для демонстрации заполним коллекции
  41. List<Entry2> lst = new List<Entry2>()
  42. {
  43. new Entry2()
  44. {
  45. Enrties =
  46. new List<Entry>()
  47. {
  48. new Entry() {Id = 1, Name = "1"},
  49. new Entry() {Id = 2, Name = "2"},
  50. new Entry() {Id = 3, Name = "3"}
  51. }
  52. },
  53. new Entry2()
  54. {
  55. Enrties =
  56. new List<Entry>()
  57. {
  58. new Entry() {Id = 3, Name = "3"},
  59. new Entry() {Id = 4, Name = "4"},
  60. new Entry() {Id = 5, Name = "5"}
  61. }
  62. },
  63. new Entry2()
  64. {
  65. Enrties =
  66. new List<Entry>()
  67. {
  68. new Entry() {Id = 5, Name = "5"},
  69. new Entry() {Id = 6, Name = "6"},
  70. new Entry() {Id = 7, Name = "7"}
  71. }
  72. },
  73. };
  74.  
  75. 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