1 //------------------------------------------------------------------------------
2 // <copyright file="EnumRowCollectionExtensions.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
10 using System
.Collections
.Generic
;
12 using System
.Linq
.Expressions
;
13 using System
.Globalization
;
14 using System
.Diagnostics
;
19 /// This static class defines the extension methods that add LINQ operator functionality
20 /// within IEnumerableDT and IOrderedEnumerableDT.
22 public static class EnumerableRowCollectionExtensions
25 /// LINQ's Where operator for generic EnumerableRowCollection.
27 public static EnumerableRowCollection
<TRow
> Where
<TRow
>(
28 this EnumerableRowCollection
<TRow
> source
,
29 Func
<TRow
, bool> predicate
)
31 EnumerableRowCollection
<TRow
> edt
=
32 new EnumerableRowCollection
<TRow
>(source
, Enumerable
.Where
<TRow
>(source
, predicate
), null); //copy constructor
33 edt
.AddPredicate(predicate
);
38 /// LINQ's OrderBy operator for generic EnumerableRowCollection.
40 public static OrderedEnumerableRowCollection
<TRow
> OrderBy
<TRow
, TKey
>(
41 this EnumerableRowCollection
<TRow
> source
,
42 Func
<TRow
, TKey
> keySelector
)
44 IEnumerable
<TRow
> ie
= Enumerable
.OrderBy
<TRow
, TKey
>(source
, keySelector
);
46 OrderedEnumerableRowCollection
<TRow
> edt
= new OrderedEnumerableRowCollection
<TRow
>(source
, ie
);
47 edt
.AddSortExpression(keySelector
, false, true);
52 /// LINQ's OrderBy operator for generic EnumerableRowCollection.
54 public static OrderedEnumerableRowCollection
<TRow
> OrderBy
<TRow
, TKey
>(
55 this EnumerableRowCollection
<TRow
> source
,
56 Func
<TRow
, TKey
> keySelector
,
57 IComparer
<TKey
> comparer
)
59 IEnumerable
<TRow
> ie
= Enumerable
.OrderBy
<TRow
, TKey
>(source
, keySelector
, comparer
);
60 OrderedEnumerableRowCollection
<TRow
> edt
= new OrderedEnumerableRowCollection
<TRow
>(source
, ie
);
61 edt
.AddSortExpression(keySelector
, comparer
, false, true);
66 /// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
68 public static OrderedEnumerableRowCollection
<TRow
> OrderByDescending
<TRow
, TKey
>(
69 this EnumerableRowCollection
<TRow
> source
,
70 Func
<TRow
, TKey
> keySelector
)
72 IEnumerable
<TRow
> ie
= Enumerable
.OrderByDescending
<TRow
, TKey
>(source
, keySelector
);
74 OrderedEnumerableRowCollection
<TRow
> edt
= new OrderedEnumerableRowCollection
<TRow
>(source
, ie
);
75 edt
.AddSortExpression(keySelector
, true, true);
80 /// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
82 public static OrderedEnumerableRowCollection
<TRow
> OrderByDescending
<TRow
, TKey
>(
83 this EnumerableRowCollection
<TRow
> source
,
84 Func
<TRow
, TKey
> keySelector
,
85 IComparer
<TKey
> comparer
)
87 IEnumerable
<TRow
> ie
= Enumerable
.OrderByDescending
<TRow
, TKey
>(source
, keySelector
, comparer
);
89 OrderedEnumerableRowCollection
<TRow
> edt
= new OrderedEnumerableRowCollection
<TRow
>(source
, ie
);
90 edt
.AddSortExpression(keySelector
, comparer
, true, true);
95 /// LINQ's ThenBy operator for generic EnumerableRowCollection.
97 public static OrderedEnumerableRowCollection
<TRow
> ThenBy
<TRow
, TKey
>(
98 this OrderedEnumerableRowCollection
<TRow
> source
,
99 Func
<TRow
, TKey
> keySelector
)
101 IEnumerable
<TRow
> ie
=
102 Enumerable
.ThenBy
<TRow
, TKey
>((IOrderedEnumerable
<TRow
>)source
.EnumerableRows
, keySelector
);
104 OrderedEnumerableRowCollection
<TRow
> edt
=
105 new OrderedEnumerableRowCollection
<TRow
>((EnumerableRowCollection
<TRow
>)source
, ie
);
107 edt
.AddSortExpression(keySelector
, /*isDesc*/ false, /*isOrderBy*/ false);
112 /// LINQ's ThenBy operator for generic EnumerableRowCollection.
114 public static OrderedEnumerableRowCollection
<TRow
> ThenBy
<TRow
, TKey
>(
115 this OrderedEnumerableRowCollection
<TRow
> source
,
116 Func
<TRow
, TKey
> keySelector
,
117 IComparer
<TKey
> comparer
)
119 IEnumerable
<TRow
> ie
=
120 Enumerable
.ThenBy
<TRow
, TKey
>((IOrderedEnumerable
<TRow
>)source
.EnumerableRows
, keySelector
, comparer
);
122 OrderedEnumerableRowCollection
<TRow
> edt
=
123 new OrderedEnumerableRowCollection
<TRow
>((EnumerableRowCollection
<TRow
>)source
, ie
);
125 edt
.AddSortExpression(keySelector
, comparer
, false, false);
130 /// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
132 public static OrderedEnumerableRowCollection
<TRow
> ThenByDescending
<TRow
, TKey
>(
133 this OrderedEnumerableRowCollection
<TRow
> source
,
134 Func
<TRow
, TKey
> keySelector
)
136 IEnumerable
<TRow
> ie
=
137 Enumerable
.ThenByDescending
<TRow
, TKey
>((IOrderedEnumerable
<TRow
>)source
.EnumerableRows
, keySelector
);
139 OrderedEnumerableRowCollection
<TRow
> edt
=
140 new OrderedEnumerableRowCollection
<TRow
>((EnumerableRowCollection
<TRow
>)source
, ie
);
142 edt
.AddSortExpression(keySelector
, /*desc*/ true, false);
147 /// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
149 public static OrderedEnumerableRowCollection
<TRow
> ThenByDescending
<TRow
, TKey
>(
150 this OrderedEnumerableRowCollection
<TRow
> source
,
151 Func
<TRow
, TKey
> keySelector
,
152 IComparer
<TKey
> comparer
)
154 IEnumerable
<TRow
> ie
=
155 Enumerable
.ThenByDescending
<TRow
, TKey
>((IOrderedEnumerable
<TRow
>)source
.EnumerableRows
, keySelector
, comparer
);
157 OrderedEnumerableRowCollection
<TRow
> edt
=
158 new OrderedEnumerableRowCollection
<TRow
>((EnumerableRowCollection
<TRow
>)source
, ie
);
160 edt
.AddSortExpression(keySelector
, comparer
, true, false);
165 /// Executes a Select (Projection) on EnumerableDataTable. If the selector returns a different
166 /// type than the type of rows, then AsLinqDataView is disabled, and the returning EnumerableDataTable
167 /// represents an enumerable over the LINQ Query.
169 public static EnumerableRowCollection
<S
> Select
<TRow
, S
>(
170 this EnumerableRowCollection
<TRow
> source
,
171 Func
<TRow
, S
> selector
)
173 //Anonymous type or some other type
174 //The only thing that matters from this point on is _enumerableRows
176 IEnumerable
<S
> typedEnumerable
= Enumerable
.Select
<TRow
, S
>(source
, selector
);
178 // Dont need predicates or sort expression from this point on since we know
179 // AsLinqDataView is disabled.
180 return new EnumerableRowCollection
<S
>(((object)source
) as EnumerableRowCollection
<S
>,
182 ((object)selector
) as Func
<S
,S
>);
186 /// Casts an EnumerableDataTable_TSource into EnumerableDataTable_TResult
188 public static EnumerableRowCollection
<TResult
> Cast
<TResult
>(this EnumerableRowCollection source
)
190 // Since Cast does not have the signature Cast_T_R(..) this call is routed
191 // through the non-generic base class EnumerableDataTable
193 if ((null != source
) && source
.ElementType
.Equals(typeof(TResult
)))
195 return (EnumerableRowCollection
<TResult
>)(object)source
;
198 { //Anonymous type or some other type
199 //The only thing that matters from this point on is _enumerableRows
201 IEnumerable
<TResult
> typedEnumerable
= Enumerable
.Cast
<TResult
>(source
);
203 EnumerableRowCollection
<TResult
> newEdt
= new EnumerableRowCollection
<TResult
>(
205 typeof(TResult
).IsAssignableFrom(source
.ElementType
) && typeof(DataRow
).IsAssignableFrom(typeof(TResult
)),