Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataSet / System / Data / EnumerableRowCollectionExtensions.cs / 1 / EnumerableRowCollectionExtensions.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
// [....]
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Globalization;
using System.Diagnostics;
namespace System.Data
{
///
/// This static class defines the extension methods that add LINQ operator functionality
/// within IEnumerableDT and IOrderedEnumerableDT.
///
public static class EnumerableRowCollectionExtensions
{
///
/// LINQ's Where operator for generic EnumerableRowCollection.
///
public static EnumerableRowCollection Where(
this EnumerableRowCollection source,
Func predicate)
{
EnumerableRowCollection edt =
new EnumerableRowCollection(source, Enumerable.Where(source, predicate), null); //copy constructor
edt.AddPredicate(predicate);
return edt;
}
///
/// LINQ's OrderBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderBy(
this EnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie = Enumerable.OrderBy(source, keySelector);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, false, true);
return edt;
}
///
/// LINQ's OrderBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderBy(
this EnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie = Enumerable.OrderBy(source, keySelector, comparer);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, comparer, false, true);
return edt;
}
///
/// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderByDescending(
this EnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie = Enumerable.OrderByDescending(source, keySelector);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, true, true);
return edt;
}
///
/// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderByDescending(
this EnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie = Enumerable.OrderByDescending(source, keySelector, comparer);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, comparer, true, true);
return edt;
}
///
/// LINQ's ThenBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenBy(
this OrderedEnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie =
Enumerable.ThenBy((IOrderedEnumerable)source.EnumerableRows, keySelector);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, /*isDesc*/ false, /*isOrderBy*/ false);
return edt;
}
///
/// LINQ's ThenBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenBy(
this OrderedEnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie =
Enumerable.ThenBy((IOrderedEnumerable)source.EnumerableRows, keySelector, comparer);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, comparer, false, false);
return edt;
}
///
/// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenByDescending(
this OrderedEnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie =
Enumerable.ThenByDescending((IOrderedEnumerable)source.EnumerableRows, keySelector);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, /*desc*/ true, false);
return edt;
}
///
/// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenByDescending(
this OrderedEnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie =
Enumerable.ThenByDescending((IOrderedEnumerable)source.EnumerableRows, keySelector, comparer);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, comparer, true, false);
return edt;
}
///
/// 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.
///
public static EnumerableRowCollection Select(
this EnumerableRowCollection source,
Func selector)
{
//Anonymous type or some other type
//The only thing that matters from this point on is _enumerableRows
IEnumerable typedEnumerable = Enumerable.Select(source, selector);
// Dont need predicates or sort expression from this point on since we know
// AsLinqDataView is disabled.
return new EnumerableRowCollection(((object)source) as EnumerableRowCollection,
typedEnumerable,
((object)selector) as Func);
}
///
/// Casts an EnumerableDataTable_TSource into EnumerableDataTable_TResult
///
public static EnumerableRowCollection Cast(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)(object)source;
}
else
{ //Anonymous type or some other type
//The only thing that matters from this point on is _enumerableRows
IEnumerable typedEnumerable = Enumerable.Cast(source);
EnumerableRowCollection newEdt = new EnumerableRowCollection(
typedEnumerable,
typeof(TResult).IsAssignableFrom(source.ElementType) && typeof(DataRow).IsAssignableFrom(typeof(TResult)),
source.Table);
return newEdt;
}
}
} //end class
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
// [....]
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Globalization;
using System.Diagnostics;
namespace System.Data
{
///
/// This static class defines the extension methods that add LINQ operator functionality
/// within IEnumerableDT and IOrderedEnumerableDT.
///
public static class EnumerableRowCollectionExtensions
{
///
/// LINQ's Where operator for generic EnumerableRowCollection.
///
public static EnumerableRowCollection Where(
this EnumerableRowCollection source,
Func predicate)
{
EnumerableRowCollection edt =
new EnumerableRowCollection(source, Enumerable.Where(source, predicate), null); //copy constructor
edt.AddPredicate(predicate);
return edt;
}
///
/// LINQ's OrderBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderBy(
this EnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie = Enumerable.OrderBy(source, keySelector);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, false, true);
return edt;
}
///
/// LINQ's OrderBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderBy(
this EnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie = Enumerable.OrderBy(source, keySelector, comparer);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, comparer, false, true);
return edt;
}
///
/// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderByDescending(
this EnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie = Enumerable.OrderByDescending(source, keySelector);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, true, true);
return edt;
}
///
/// LINQ's OrderByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection OrderByDescending(
this EnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie = Enumerable.OrderByDescending(source, keySelector, comparer);
OrderedEnumerableRowCollection edt = new OrderedEnumerableRowCollection(source, ie);
edt.AddSortExpression(keySelector, comparer, true, true);
return edt;
}
///
/// LINQ's ThenBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenBy(
this OrderedEnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie =
Enumerable.ThenBy((IOrderedEnumerable)source.EnumerableRows, keySelector);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, /*isDesc*/ false, /*isOrderBy*/ false);
return edt;
}
///
/// LINQ's ThenBy operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenBy(
this OrderedEnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie =
Enumerable.ThenBy((IOrderedEnumerable)source.EnumerableRows, keySelector, comparer);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, comparer, false, false);
return edt;
}
///
/// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenByDescending(
this OrderedEnumerableRowCollection source,
Func keySelector)
{
IEnumerable ie =
Enumerable.ThenByDescending((IOrderedEnumerable)source.EnumerableRows, keySelector);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, /*desc*/ true, false);
return edt;
}
///
/// LINQ's ThenByDescending operator for generic EnumerableRowCollection.
///
public static OrderedEnumerableRowCollection ThenByDescending(
this OrderedEnumerableRowCollection source,
Func keySelector,
IComparer comparer)
{
IEnumerable ie =
Enumerable.ThenByDescending((IOrderedEnumerable)source.EnumerableRows, keySelector, comparer);
OrderedEnumerableRowCollection edt =
new OrderedEnumerableRowCollection((EnumerableRowCollection)source, ie);
edt.AddSortExpression(keySelector, comparer, true, false);
return edt;
}
///
/// 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.
///
public static EnumerableRowCollection Select(
this EnumerableRowCollection source,
Func selector)
{
//Anonymous type or some other type
//The only thing that matters from this point on is _enumerableRows
IEnumerable typedEnumerable = Enumerable.Select(source, selector);
// Dont need predicates or sort expression from this point on since we know
// AsLinqDataView is disabled.
return new EnumerableRowCollection(((object)source) as EnumerableRowCollection,
typedEnumerable,
((object)selector) as Func);
}
///
/// Casts an EnumerableDataTable_TSource into EnumerableDataTable_TResult
///
public static EnumerableRowCollection Cast(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)(object)source;
}
else
{ //Anonymous type or some other type
//The only thing that matters from this point on is _enumerableRows
IEnumerable typedEnumerable = Enumerable.Cast(source);
EnumerableRowCollection newEdt = new EnumerableRowCollection(
typedEnumerable,
typeof(TResult).IsAssignableFrom(source.ElementType) && typeof(DataRow).IsAssignableFrom(typeof(TResult)),
source.Table);
return newEdt;
}
}
} //end class
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ImportOptions.cs
- JsonFaultDetail.cs
- ConditionalDesigner.cs
- CompiledQueryCacheKey.cs
- BigInt.cs
- GestureRecognitionResult.cs
- IndexExpression.cs
- PasswordBox.cs
- HttpRuntime.cs
- WindowsToolbar.cs
- SingleAnimationUsingKeyFrames.cs
- MouseOverProperty.cs
- TraceHandlerErrorFormatter.cs
- RoutedUICommand.cs
- ApplicationHost.cs
- SourceFilter.cs
- AttributeCollection.cs
- TypeTypeConverter.cs
- ConnectionStringEditor.cs
- ScriptResourceAttribute.cs
- FigureParagraph.cs
- ActivityInstance.cs
- XmlKeywords.cs
- GridViewColumnCollection.cs
- TriggerBase.cs
- PartBasedPackageProperties.cs
- RoleService.cs
- Control.cs
- WorkflowInstanceProxy.cs
- DictionaryItemsCollection.cs
- MasterPageBuildProvider.cs
- InstanceKeyNotReadyException.cs
- TableDetailsCollection.cs
- X509Chain.cs
- Pointer.cs
- CodeMethodReturnStatement.cs
- ProxyFragment.cs
- SignatureResourcePool.cs
- Helpers.cs
- Matrix3DConverter.cs
- SqlUtil.cs
- WindowsAuthenticationEventArgs.cs
- OutputCacheSettings.cs
- WebPartDesigner.cs
- PointCollectionConverter.cs
- TrackingServices.cs
- XmlAttribute.cs
- InstancePersistenceEvent.cs
- Win32MouseDevice.cs
- FileReservationCollection.cs
- LabelAutomationPeer.cs
- ScaleTransform3D.cs
- ScriptResourceAttribute.cs
- BypassElement.cs
- EntryWrittenEventArgs.cs
- ToolStripDropTargetManager.cs
- GatewayIPAddressInformationCollection.cs
- HotCommands.cs
- FileReservationCollection.cs
- DefaultValueAttribute.cs
- configsystem.cs
- KeySplineConverter.cs
- RowToParametersTransformer.cs
- _ContextAwareResult.cs
- TextRangeEditLists.cs
- MenuAdapter.cs
- ExchangeUtilities.cs
- XmlSchemaIdentityConstraint.cs
- TreeNodeBinding.cs
- ChildTable.cs
- XmlFormatMapping.cs
- BaseAppDomainProtocolHandler.cs
- ItemChangedEventArgs.cs
- TypeResolver.cs
- WebPartConnectionsCancelEventArgs.cs
- XamlToRtfWriter.cs
- ConnectionProviderAttribute.cs
- Rect3D.cs
- IsolatedStorage.cs
- DesignerTransactionCloseEvent.cs
- SectionUpdates.cs
- UIPermission.cs
- CompositeScriptReference.cs
- securitycriticaldataClass.cs
- ObjectManager.cs
- ZipIOExtraFieldPaddingElement.cs
- UIElementAutomationPeer.cs
- XslVisitor.cs
- TreeNodeMouseHoverEvent.cs
- DataMemberAttribute.cs
- safelink.cs
- XmlStringTable.cs
- DiscoveryMessageSequence.cs
- ExtensionsSection.cs
- sqlinternaltransaction.cs
- PageBuildProvider.cs
- GlyphElement.cs
- AppDomainCompilerProxy.cs
- ContractCodeDomInfo.cs
- BrowserCapabilitiesFactory.cs