■ 객체 컬렉션에서 특정 객체 속성 값의 집합을 구하는 방법을 보여준다. (속성 2개 이상 지정 가능)
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; #region 객체 컬렉션 특정 객체 속성 값의 집합 구하기 - GetDistinctCollection(sourceCollection, propertyNameArray) /// <summary> /// 객체 컬렉션 특정 객체 속성 값의 집합 구하기 /// </summary> /// <param name="sourceCollection">소스 컬렉션</param> /// <param name="propertyNameArray">속성명 배열</param> /// <returns>객체 컬렉션 특정 객체 속성 값의 집합</returns> public ObservableCollection<object> GetDistinctCollection(ObservableCollection<object> sourceCollection, string[] propertyNameArray) { if(sourceCollection == null || sourceCollection.Count == 0) { return null; } var result = sourceCollection.AsEnumerable().GroupBy(item => new DistinctTuple<object>(from propertyName in propertyNameArray select item.GetType().GetProperty(propertyName).GetValue(item))); int propertyNameCount = propertyNameArray.Length; PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(sourceCollection[0]); DynamicClass dynamicClass = new DynamicClass(); // '동적 클래스 사용하기' 참조 for(int i = 0; i < propertyNameCount; i++) { dynamicClass.AddProperty(propertyNameArray[i], pdc[propertyNameArray[i]].PropertyType); } dynamicClass.CreateType(); ObservableCollection<object> targetCollection = new ObservableCollection<object>(); foreach(var group in result) { dynamicClass.CreateInstance(); for(int i = 0; i < propertyNameCount; i++) { dynamicClass.SetValue(i, group.Key.ValueArray[i]); } targetCollection.Add(dynamicClass.Instance); } return targetCollection; } #endregion /// <summary> /// 중복 제거 튜플 /// </summary> /// <typeparam name="T">타입</typeparam> public class DistinctTuple<T> : IEquatable<DistinctTuple<T>> { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 값 배열 /// </summary> public readonly T[] ValueArray; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DistinctTuple(enumerable) /// <summary> /// 생성자 /// </summary> /// <param name="enumerable">열거 가능형</param> public DistinctTuple(IEnumerable<T> enumerable) { ValueArray = enumerable.ToArray(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 동등 여부 조사하기 - Equals(sourceTuple) /// <summary> /// 동등 여부 조사하기 /// </summary> /// <param name="sourceTuple">소스 Tuple</param> /// <returns>동등 여부</returns> public bool Equals(DistinctTuple<T> sourceTuple) { if(ReferenceEquals(this, sourceTuple)) { return true; } if(sourceTuple == null) { return false; } int length = ValueArray.Length; if(length != sourceTuple.ValueArray.Length) { return false; } for(int i = 0; i < length; i++) { if(!Equals(ValueArray[i], sourceTuple.ValueArray[i])) { return false; } } return true; } #endregion #region 동등 여부 조사하기 - Equals(sourceObject) /// <summary> /// 동등 여부 조사하기 /// </summary> /// <param name="sourceObject">소스 객체</param> /// <returns>동등 여부</returns> public override bool Equals(object sourceObject) { if(ReferenceEquals(this, sourceObject)) { return false; } if(sourceObject == null) { return false; } return Equals(sourceObject as DistinctTuple<T>); } #endregion #region 해시 코드 구하기 - GetHashCode() /// <summary> /// 해시 코드 구하기 /// </summary> /// <returns>해시 코드</returns> public override int GetHashCode() { int hashCode = 17; foreach(var value in ValueArray) { hashCode = hashCode * 37 + (!ReferenceEquals(value, null) ? value.GetHashCode() : 0); } return hashCode; } #endregion } |