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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
//------------------------------------------------------------------------------
// <copyright file="EnumRowCollectionExtensions.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Globalization;
using System.Diagnostics;
namespace System.Data
{
/// <summary>
/// This static class defines the extension methods that add LINQ operator functionality
/// within IEnumerableDT and IOrderedEnumerableDT.
/// </summary>
public static class EnumerableRowCollectionExtensions
{
/// <summary>
/// LINQ's Where operator for generic EnumerableRowCollection.
/// </summary>
public static EnumerableRowCollection<TRow> Where<TRow>(
this EnumerableRowCollection<TRow> source,
Func<TRow, bool> predicate)
{
EnumerableRowCollection<TRow> edt =
new EnumerableRowCollection<TRow>(source, Enumerable.Where<TRow>(source, predicate), null); //copy constructor
edt.AddPredicate(predicate);
return edt;
}
/// <summary>
/// LINQ's OrderBy operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> OrderBy<TRow, TKey>(
this EnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector)
{
IEnumerable<TRow> ie = Enumerable.OrderBy<TRow, TKey>(source, keySelector);
OrderedEnumerableRowCollection<TRow> edt = new OrderedEnumerableRowCollection<TRow>(source, ie);
edt.AddSortExpression(keySelector, false, true);
return edt;
}
/// <summary>
/// LINQ's OrderBy operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> OrderBy<TRow, TKey>(
this EnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector,
IComparer<TKey> comparer)
{
IEnumerable<TRow> ie = Enumerable.OrderBy<TRow, TKey>(source, keySelector, comparer);
OrderedEnumerableRowCollection<TRow> edt = new OrderedEnumerableRowCollection<TRow>(source, ie);
edt.AddSortExpression(keySelector, comparer, false, true);
return edt;
}
/// <summary>
/// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> OrderByDescending<TRow, TKey>(
this EnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector)
{
IEnumerable<TRow> ie = Enumerable.OrderByDescending<TRow, TKey>(source, keySelector);
OrderedEnumerableRowCollection<TRow> edt = new OrderedEnumerableRowCollection<TRow>(source, ie);
edt.AddSortExpression(keySelector, true, true);
return edt;
}
/// <summary>
/// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> OrderByDescending<TRow, TKey>(
this EnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector,
IComparer<TKey> comparer)
{
IEnumerable<TRow> ie = Enumerable.OrderByDescending<TRow, TKey>(source, keySelector, comparer);
OrderedEnumerableRowCollection<TRow> edt = new OrderedEnumerableRowCollection<TRow>(source, ie);
edt.AddSortExpression(keySelector, comparer, true, true);
return edt;
}
/// <summary>
/// LINQ's ThenBy operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> ThenBy<TRow, TKey>(
this OrderedEnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector)
{
IEnumerable<TRow> ie =
Enumerable.ThenBy<TRow, TKey>((IOrderedEnumerable<TRow>)source.EnumerableRows, keySelector);
OrderedEnumerableRowCollection<TRow> edt =
new OrderedEnumerableRowCollection<TRow>((EnumerableRowCollection<TRow>)source, ie);
edt.AddSortExpression(keySelector, /*isDesc*/ false, /*isOrderBy*/ false);
return edt;
}
/// <summary>
/// LINQ's ThenBy operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> ThenBy<TRow, TKey>(
this OrderedEnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector,
IComparer<TKey> comparer)
{
IEnumerable<TRow> ie =
Enumerable.ThenBy<TRow, TKey>((IOrderedEnumerable<TRow>)source.EnumerableRows, keySelector, comparer);
OrderedEnumerableRowCollection<TRow> edt =
new OrderedEnumerableRowCollection<TRow>((EnumerableRowCollection<TRow>)source, ie);
edt.AddSortExpression(keySelector, comparer, false, false);
return edt;
}
/// <summary>
/// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> ThenByDescending<TRow, TKey>(
this OrderedEnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector)
{
IEnumerable<TRow> ie =
Enumerable.ThenByDescending<TRow, TKey>((IOrderedEnumerable<TRow>)source.EnumerableRows, keySelector);
OrderedEnumerableRowCollection<TRow> edt =
new OrderedEnumerableRowCollection<TRow>((EnumerableRowCollection<TRow>)source, ie);
edt.AddSortExpression(keySelector, /*desc*/ true, false);
return edt;
}
/// <summary>
/// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
/// </summary>
public static OrderedEnumerableRowCollection<TRow> ThenByDescending<TRow, TKey>(
this OrderedEnumerableRowCollection<TRow> source,
Func<TRow, TKey> keySelector,
IComparer<TKey> comparer)
{
IEnumerable<TRow> ie =
Enumerable.ThenByDescending<TRow, TKey>((IOrderedEnumerable<TRow>)source.EnumerableRows, keySelector, comparer);
OrderedEnumerableRowCollection<TRow> edt =
new OrderedEnumerableRowCollection<TRow>((EnumerableRowCollection<TRow>)source, ie);
edt.AddSortExpression(keySelector, comparer, true, false);
return edt;
}
/// <summary>
/// Executes a Select (Projection) on EnumerableDataTable. If the selector returns a different
/// type than the type of rows, then AsLinqDataView is disabled, and the returning EnumerableDataTable
/// represents an enumerable over the LINQ Query.
/// </summary>
public static EnumerableRowCollection<S> Select<TRow, S>(
this EnumerableRowCollection<TRow> source,
Func<TRow, S> selector)
{
//Anonymous type or some other type
//The only thing that matters from this point on is _enumerableRows
IEnumerable<S> typedEnumerable = Enumerable.Select<TRow, S>(source, selector);
// Dont need predicates or sort expression from this point on since we know
// AsLinqDataView is disabled.
return new EnumerableRowCollection<S>(((object)source) as EnumerableRowCollection<S>,
typedEnumerable,
((object)selector) as Func<S,S>);
}
/// <summary>
/// Casts an EnumerableDataTable_TSource into EnumerableDataTable_TResult
/// </summary>
public static EnumerableRowCollection<TResult> Cast<TResult>(this EnumerableRowCollection source)
{
// Since Cast does not have the signature Cast_T_R(..) this call is routed
// through the non-generic base class EnumerableDataTable
if ((null != source) && source.ElementType.Equals(typeof(TResult)))
{
return (EnumerableRowCollection<TResult>)(object)source;
}
else
{ //Anonymous type or some other type
//The only thing that matters from this point on is _enumerableRows
IEnumerable<TResult> typedEnumerable = Enumerable.Cast<TResult>(source);
EnumerableRowCollection<TResult> newEdt = new EnumerableRowCollection<TResult>(
typedEnumerable,
typeof(TResult).IsAssignableFrom(source.ElementType) && typeof(DataRow).IsAssignableFrom(typeof(TResult)),
source.Table);
return newEdt;
}
}
} //end class
}
|