markus / KCOM / Extensions / LinqExtension.cs @ 2007ecaa
이력 | 보기 | 이력해설 | 다운로드 (2.05 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
|
7 |
namespace KCOM |
8 |
{ |
9 |
public static class Extensions |
10 |
{ |
11 |
public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate) |
12 |
{ |
13 |
if (condition) |
14 |
return source.Where(predicate); |
15 |
else |
16 |
return source; |
17 |
} |
18 |
|
19 |
public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, int, bool> predicate) |
20 |
{ |
21 |
if (condition) |
22 |
return source.Where(predicate); |
23 |
else |
24 |
return source; |
25 |
} |
26 |
|
27 |
public static bool TryItem<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, int, bool> predicate, out TSource item) |
28 |
{ |
29 |
bool result = false; |
30 |
item = default(TSource); |
31 |
|
32 |
if (condition) |
33 |
{ |
34 |
var selectItem = source.Where(predicate); |
35 |
|
36 |
if (selectItem.Count() > 0) |
37 |
{ |
38 |
item = selectItem.First(); |
39 |
result = true; |
40 |
} |
41 |
} |
42 |
|
43 |
return result; |
44 |
} |
45 |
|
46 |
/// <summary> |
47 |
/// |
48 |
/// </summary> |
49 |
/// <typeparam name="TSource"></typeparam> |
50 |
/// <param name="source"></param> |
51 |
/// <param name="item"></param> |
52 |
/// <returns></returns> |
53 |
public static bool TryFrist<TSource>(this List<TSource> source,bool RemoveSource, out TSource item) |
54 |
{ |
55 |
bool result = false; |
56 |
item = default(TSource); |
57 |
|
58 |
if (source.Count() > 0) |
59 |
{ |
60 |
var selectItem = source.First(); |
61 |
|
62 |
item = selectItem; |
63 |
|
64 |
if (RemoveSource) |
65 |
{ |
66 |
source.Remove(selectItem); |
67 |
} |
68 |
|
69 |
result = true; |
70 |
} |
71 |
|
72 |
return result; |
73 |
} |
74 |
} |
75 |
} |