markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / CollectionExtenstions.cs @ 2decfbdf
이력 | 보기 | 이력해설 | 다운로드 (8.11 KB)
1 | 0157b158 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Linq.Expressions; |
||
5 | using System.Text; |
||
6 | using System.Threading.Tasks; |
||
7 | |||
8 | namespace Markus.Service.Extensions |
||
9 | { |
||
10 | public static class CollectionExtensions |
||
11 | { |
||
12 | 0a89a17f | taeseongkim | //public static void Update<T>(this IList<T> source,IList<T> Target,IList<string> Keys) |
13 | //{ |
||
14 | // for (int i = source.Count; i == 0 ; --i) |
||
15 | // { |
||
16 | // var item = source[i]; |
||
17 | // Target.Where(f=>) |
||
18 | // item.GetType().GetProperty() |
||
19 | // } |
||
20 | //} |
||
21 | |||
22 | /// <summary> |
||
23 | /// Changes all elements of IEnumerable by the change function |
||
24 | /// </summary> |
||
25 | /// <param name="enumerable">The enumerable where you want to change stuff</param> |
||
26 | /// <param name="change">The way you want to change the stuff</param> |
||
27 | /// <returns>An IEnumerable with all changes applied</returns> |
||
28 | public static IEnumerable<T> Change<T>(this IEnumerable<T> enumerable, Func<T, T> change) |
||
29 | { |
||
30 | ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
||
31 | ArgumentCheck.IsNullorWhiteSpace(change, "change"); |
||
32 | |||
33 | foreach (var item in enumerable) |
||
34 | { |
||
35 | yield return change(item); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /// <summary> |
||
40 | /// Changes all elements of IEnumerable by the change function, that fullfill the where function |
||
41 | /// </summary> |
||
42 | /// <param name="enumerable">The enumerable where you want to change stuff</param> |
||
43 | /// <param name="change">The way you want to change the stuff</param> |
||
44 | /// <param name="where">The function to check where changes should be made</param> |
||
45 | /// <returns> |
||
46 | /// An IEnumerable with all changes applied |
||
47 | /// </returns> |
||
48 | public static IEnumerable<T> ChangeWhere<T>(this IEnumerable<T> enumerable, |
||
49 | Func<T, T> change, |
||
50 | Func<T, bool> @where) |
||
51 | { |
||
52 | ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
||
53 | ArgumentCheck.IsNullorWhiteSpace(change, "change"); |
||
54 | ArgumentCheck.IsNullorWhiteSpace(@where, "where"); |
||
55 | |||
56 | foreach (var item in enumerable) |
||
57 | { |
||
58 | if (@where(item)) |
||
59 | { |
||
60 | yield return change(item); |
||
61 | } |
||
62 | else |
||
63 | { |
||
64 | yield return item; |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public static class ArgumentCheck |
||
70 | 0157b158 | taeseongkim | { |
71 | 0a89a17f | taeseongkim | |
72 | |||
73 | /// <summary> |
||
74 | /// Checks if a value is string or any other object if it is string |
||
75 | /// it checks for nullorwhitespace otherwhise it checks for null only |
||
76 | /// </summary> |
||
77 | /// <typeparam name="T">Type of the item you want to check</typeparam> |
||
78 | /// <param name="item">The item you want to check</param> |
||
79 | /// <param name="nameOfTheArgument">Name of the argument</param> |
||
80 | public static void IsNullorWhiteSpace<T>(T item, string nameOfTheArgument = "") |
||
81 | { |
||
82 | |||
83 | Type type = typeof(T); |
||
84 | if (type == typeof(string) || |
||
85 | type == typeof(String)) |
||
86 | { |
||
87 | if (string.IsNullOrWhiteSpace(item as string)) |
||
88 | { |
||
89 | throw new ArgumentException(nameOfTheArgument + " is null or Whitespace"); |
||
90 | } |
||
91 | } |
||
92 | else |
||
93 | { |
||
94 | if (item == null) |
||
95 | { |
||
96 | throw new ArgumentException(nameOfTheArgument + " is null"); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | } |
||
101 | } |
||
102 | |||
103 | /// <summary> |
||
104 | /// Changes all elements of IEnumerable by the change function that do not fullfill the except function |
||
105 | /// </summary> |
||
106 | /// <param name="enumerable">The enumerable where you want to change stuff</param> |
||
107 | /// <param name="change">The way you want to change the stuff</param> |
||
108 | /// <param name="where">The function to check where changes should not be made</param> |
||
109 | /// <returns> |
||
110 | /// An IEnumerable with all changes applied |
||
111 | /// </returns> |
||
112 | public static IEnumerable<T> ChangeExcept<T>(this IEnumerable<T> enumerable, |
||
113 | Func<T, T> change, |
||
114 | Func<T, bool> @where) |
||
115 | { |
||
116 | ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
||
117 | ArgumentCheck.IsNullorWhiteSpace(change, "change"); |
||
118 | ArgumentCheck.IsNullorWhiteSpace(@where, "where"); |
||
119 | |||
120 | foreach (var item in enumerable) |
||
121 | { |
||
122 | if (!@where(item)) |
||
123 | { |
||
124 | yield return change(item); |
||
125 | } |
||
126 | else |
||
127 | { |
||
128 | yield return item; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /// <summary> |
||
134 | /// Update all elements of IEnumerable by the update function (only works with reference types) |
||
135 | /// </summary> |
||
136 | /// <param name="enumerable">The enumerable where you want to change stuff</param> |
||
137 | /// <param name="update">The way you want to change the stuff</param> |
||
138 | /// <returns> |
||
139 | /// The same enumerable you passed in |
||
140 | /// </returns> |
||
141 | public static IEnumerable<T> Update<T>(this IEnumerable<T> enumerable, |
||
142 | Action<T> update) where T : class |
||
143 | { |
||
144 | ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
||
145 | ArgumentCheck.IsNullorWhiteSpace(update, "update"); |
||
146 | foreach (var item in enumerable) |
||
147 | { |
||
148 | update(item); |
||
149 | } |
||
150 | return enumerable; |
||
151 | } |
||
152 | |||
153 | /// <summary> |
||
154 | /// Update all elements of IEnumerable by the update function (only works with reference types) |
||
155 | /// where the where function returns true |
||
156 | /// </summary> |
||
157 | /// <param name="enumerable">The enumerable where you want to change stuff</param> |
||
158 | /// <param name="update">The way you want to change the stuff</param> |
||
159 | /// <param name="where">The function to check where updates should be made</param> |
||
160 | /// <returns> |
||
161 | /// The same enumerable you passed in |
||
162 | /// </returns> |
||
163 | public static IEnumerable<T> UpdateWhere<T>(this IEnumerable<T> enumerable, |
||
164 | Action<T> update, Func<T, bool> where) where T : class |
||
165 | { |
||
166 | ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
||
167 | ArgumentCheck.IsNullorWhiteSpace(update, "update"); |
||
168 | foreach (var item in enumerable) |
||
169 | { |
||
170 | a6e5055d | alzkakdixm | if (where(item))//UpdateWhere에서 맨마지막 조건 |
171 | 2decfbdf | alzkakdixm | {//그리드 collection 돌면서 where절 조건 만족하면 update |
172 | update(item);//그리드 status와 디비 status 같으면 업데이트 |
||
173 | }//업데이트 함수 |
||
174 | 0a89a17f | taeseongkim | } |
175 | return enumerable; |
||
176 | } |
||
177 | |||
178 | /// <summary> |
||
179 | /// Update all elements of IEnumerable by the update function (only works with reference types) |
||
180 | /// Except the elements from the where function |
||
181 | /// </summary> |
||
182 | /// <param name="enumerable">The enumerable where you want to change stuff</param> |
||
183 | /// <param name="update">The way you want to change the stuff</param> |
||
184 | /// <param name="where">The function to check where changes should not be made</param> |
||
185 | /// <returns> |
||
186 | /// The same enumerable you passed in |
||
187 | /// </returns> |
||
188 | public static IEnumerable<T> UpdateExcept<T>(this IEnumerable<T> enumerable, |
||
189 | Action<T> update, Func<T, bool> where) where T : class |
||
190 | { |
||
191 | ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable"); |
||
192 | ArgumentCheck.IsNullorWhiteSpace(update, "update"); |
||
193 | |||
194 | foreach (var item in enumerable) |
||
195 | { |
||
196 | if (!where(item)) |
||
197 | { |
||
198 | update(item); |
||
199 | } |
||
200 | } |
||
201 | return enumerable; |
||
202 | 0157b158 | taeseongkim | } |
203 | } |
||
204 | } |