System Linq
System Linq
System.Linq
Enumerable
Aggregate
All
Any
Append
AsEnumerable
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Empty
Except
First
FirstOrDefault
GroupBy
GroupJoin
Intersect
Join
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Prepend
Range
Repeat
Reverse
Select
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipLast
SkipWhile
Sum
Take
TakeLast
TakeWhile
ThenBy
ThenByDescending
ToArray
ToDictionary
ToHashSet
ToList
ToLookup
Union
Where
Zip
EnumerableExecutor
EnumerableExecutor
EnumerableExecutor<T>
EnumerableExecutor<T>
EnumerableExecutor<T>
EnumerableQuery
EnumerableQuery
EnumerableQuery<T>
EnumerableQuery<T>
ToString
IGrouping<TKey,TElement>
Key
ILookup<TKey,TElement>
Contains
Count
Item
ImmutableArrayExtensions
Aggregate
All
Any
ElementAt
ElementAtOrDefault
First
FirstOrDefault
Last
LastOrDefault
Select
SelectMany
SequenceEqual
Single
SingleOrDefault
ToArray
ToDictionary
Where
IOrderedEnumerable<TElement>
CreateOrderedEnumerable
IOrderedQueryable
IOrderedQueryable<T>
IQueryable
ElementType
Expression
Provider
IQueryable<T>
IQueryProvider
CreateQuery
Execute
Lookup<TKey,TElement>
ApplyResultSelector
Contains
Count
GetEnumerator
Item
OrderedParallelQuery<TSource>
GetEnumerator
ParallelEnumerable
Aggregate
All
Any
AsEnumerable
AsOrdered
AsParallel
AsSequential
AsUnordered
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Empty
Except
First
FirstOrDefault
ForAll
GroupBy
GroupJoin
Intersect
Join
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Range
Repeat
Reverse
Select
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipWhile
Sum
Take
TakeWhile
ThenBy
ThenByDescending
ToArray
ToDictionary
ToList
ToLookup
Union
Where
WithCancellation
WithDegreeOfParallelism
WithExecutionMode
WithMergeOptions
Zip
ParallelExecutionMode
Default
ForceParallelism
ParallelMergeOptions
AutoBuffered
Default
FullyBuffered
NotBuffered
ParallelQuery
ParallelQuery<TSource>
GetEnumerator
Queryable
Aggregate
All
Any
Append
AsQueryable
Average
Cast
Concat
Contains
Count
DefaultIfEmpty
Distinct
ElementAt
ElementAtOrDefault
Except
First
FirstOrDefault
GroupBy
GroupJoin
Intersect
Join
Last
LastOrDefault
LongCount
Max
Min
OfType
OrderBy
OrderByDescending
Prepend
Reverse
Select
SelectMany
SequenceEqual
Single
SingleOrDefault
Skip
SkipLast
SkipWhile
Sum
Take
TakeLast
TakeWhile
ThenBy
ThenByDescending
Union
Where
Zip
System.Linq Namespace
The System.Linq namespace provides classes and interfaces that support queries that use Language-Integrated Query
(LINQ).
Introduction
The System.Linq namespace is in the System.Core assembly (in System.Core.dll).
The Enumerable class contains LINQ standard query operators that operate on objects that implement
IEnumerable<T>.
The Queryable class contains LINQ standard query operators that operate on objects that implement IQueryable<T>.
For information about LINQ to SQL, see LINQ to SQL [LINQ to SQL].
Classes
Enumerable
Provides a set of static ( Shared in Visual Basic) methods
for querying objects that implement IEnumerable<T>.
EnumerableExecutor
Represents an expression tree and provides functionality to
execute the expression tree after rewriting it.
EnumerableExecutor<T>
Represents an expression tree and provides functionality to
execute the expression tree after rewriting it.
EnumerableQuery
Represents an IEnumerable as an EnumerableQuery data
source.
EnumerableQuery<T>
Represents an IEnumerable<T> collection as an
IQueryable<T> data source.
ImmutableArrayExtensions
LINQ extension method overrides that offer greater efficiency
for ImmutableArray<T> than the standard LINQ methods
NuGet package: System.Collections.Immutable (about
immutable collections and how to install)
Lookup<TKey,TElement>
Represents a collection of keys each mapped to one or more
values.
OrderedParallelQuery<TSource>
Represents a sorted, parallel sequence.
ParallelEnumerable
Provides a set of methods for querying objects that implement
ParallelQuery{TSource}. This is the parallel equivalent of
Enumerable.
ParallelQuery
Represents a parallel sequence.
ParallelQuery<TSource>
Represents a parallel sequence.
Queryable
Provides a set of static ( Shared in Visual Basic) methods
for querying data structures that implement IQueryable<T>.
Interfaces
IGrouping<TKey,TElement>
Represents a collection of objects that have a common key.
ILookup<TKey,TElement>
Defines an indexer, size property, and Boolean search method
for data structures that map keys to IEnumerable<T>
sequences of values.
IOrderedEnumerable<TElement>
Represents a sorted sequence.
IOrderedQueryable
Represents the result of a sorting operation.
IOrderedQueryable<T>
Represents the result of a sorting operation.
IQueryable
Provides functionality to evaluate queries against a specific
data source wherein the type of the data is not specified.
IQueryable<T>
Provides functionality to evaluate queries against a specific
data source wherein the type of the data is known.
IQueryProvider
Defines methods to create and execute queries that are
described by an IQueryable object.
Enums
ParallelExecutionMode
The query execution mode is a hint that specifies how the
system should handle performance trade-offs when
parallelizing queries.
ParallelMergeOptions
Specifies the preferred type of output merge to use in a query.
In other words, it indicates how PLINQ should merge the
results from the various partitions back into a single result
sequence. This is a hint only, and may not be respected by the
system when parallelizing all queries.
Enumerable Class
Provides a set of static ( Shared in Visual Basic) methods for querying objects that implement IEnumerable<T>.
D eclaration
public static class Enumerable
I nheritance H ierarchy
Object
Remarks
The methods in this class provide an implementation of the standard query operators for querying data sources that
implement IEnumerable<T>. The standard query operators are general purpose methods that follow the LINQ pattern
and enable you to express traversal, filter, and projection operations over data in any .NET-based programming
language.
The majority of the methods in this class are defined as extension methods that extend IEnumerable<T>. This means
they can be called like an instance method on any object that implements IEnumerable<T>.
Methods that are used in a query that returns a sequence of values do not consume the target data until the query
object is enumerated. This is known as deferred execution. Methods that are used in a query that returns a singleton
value execute and consume the target data immediately.
Methods
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value,
and the specified function is used to select the result value.
D eclaration
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to aggregate over.
seed TAccumulate
The initial accumulator value.
func System.Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
resultSelector System.Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
D eclaration
public static TAccumulate Aggregate<TSource,TAccumulate> (this
System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> func);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to aggregate over.
seed TAccumulate
The initial accumulator value.
func System.Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
Returns
TAccumulate
The final accumulator value.
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to aggregate over.
func System.Func<TSource,TSource,TSource>
An accumulator function to be invoked on each element.
Returns
TSource
The final accumulator value.
All<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Determines whether all elements of a sequence satisfy a condition.
D eclaration
public static bool All<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements to apply the predicate to.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false .
Any<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to check for emptiness.
Returns
Boolean
true if the source sequence contains any elements; otherwise, false .
Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to apply the predicate to.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Boolean
true if any elements in the source sequence pass the test in the specified predicate; otherwise, false .
Append<TSource>(IEnumerable<TSource>, TSource)
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Append<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, TSource element);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
element TSource
Returns
System.Collections.Generic.IEnumerable<TSource>
AsEnumerable<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to type as IEnumerable<T>.
Returns
System.Collections.Generic.IEnumerable<TSource>
The input sequence typed as IEnumerable<T>.
Average(IEnumerable<Single>)
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to calculate the average of.
Returns
Single
The average of the sequence of values.
Average(IEnumerable<Nullable<Single>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to calculate the average of.
Returns
System.Nullable<System.Single>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average(IEnumerable<Nullable<Int64>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to calculate the average of.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average(IEnumerable<Nullable<Int32>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to calculate the average of.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average(IEnumerable<Nullable<Double>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to calculate the average of.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average(IEnumerable<Int64>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Average(IEnumerable<Int32>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Average(IEnumerable<Double>)
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to calculate the average of.
Returns
Double
The average of the sequence of values.
Average(IEnumerable<Decimal>)
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to calculate the average of.
Returns
Decimal
The average of the sequence of values.
Average(IEnumerable<Nullable<Decimal>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to calculate the average of.
Returns
System.Nullable<System.Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Computes the average of a sequence of nullable Int32 values that are obtained by invoking a transform function
on each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<int>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Computes the average of a sequence of Single values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static float Average<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,float> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The average of the sequence of values.
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Computes the average of a sequence of nullable Single values that are obtained by invoking a transform function
on each element of the input sequence.
D eclaration
public static Nullable<float> Average<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<float>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Computes the average of a sequence of nullable Int64 values that are obtained by invoking a transform function
on each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<long>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Computes the average of a sequence of nullable Double values that are obtained by invoking a transform function
on each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<double>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Computes the average of a sequence of nullable Decimal values that are obtained by invoking a transform
function on each element of the input sequence.
D eclaration
public static Nullable<decimal> Average<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<decimal>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Average<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Computes the average of a sequence of Int64 values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,long> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Computes the average of a sequence of Int32 values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,int> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Computes the average of a sequence of Double values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,double> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Computes the average of a sequence of Decimal values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static decimal Average<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,decimal> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate an average.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The average of the sequence of values.
Cast<TResult>(IEnumerable)
Parameters
source IEnumerable
The IEnumerable that contains the elements to be cast to type TResult .
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains each element of the source sequence cast to the specified type.
Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Parameters
first System.Collections.Generic.IEnumerable<TSource>
The first sequence to concatenate.
second System.Collections.Generic.IEnumerable<TSource>
The sequence to concatenate to the first sequence.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the concatenated elements of the two input sequences.
Contains<TSource>(IEnumerable<TSource>, TSource)
Determines whether a sequence contains a specified element by using the default equality comparer.
D eclaration
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, TSource value);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An equality comparer to compare values.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false .
Count<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence that contains elements to be counted.
Returns
Int32
The number of elements in the input sequence.
Count<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns a number that represents how many elements in the specified sequence satisfy a condition.
D eclaration
public static int Count<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence that contains elements to be tested and counted.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Int32
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is
empty.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return the specified value for if it is empty.
defaultValue TSource
The value to return if the sequence is empty.
Returns
System.Collections.Generic.IEnumerable<TSource>
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains defaultValue if source is empty; otherwise, source .
DefaultIfEmpty<TSource>(IEnumerable<TSource>)
Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the
sequence is empty.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return a default value for if it is empty.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> object that contains the default value for the TSource type if source is empty; otherwise, source .
Distinct<TSource>(IEnumerable<TSource>)
Returns distinct elements from a sequence by using the default equality comparer to compare values.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to remove duplicate elements from.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains distinct elements from the source sequence.
Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to remove duplicate elements from.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains distinct elements from the source sequence.
ElementAt<TSource>(IEnumerable<TSource>, Int32)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
The element at the specified position in the source sequence.
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32)
Returns the element at a specified index in a sequence or a default value if the index is out of range.
D eclaration
public static TSource ElementAtOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int index);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
default ( TSource ) if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the
source sequence.
Empty<TResult>()
Returns
System.Collections.Generic.IEnumerable<TResult>
An empty IEnumerable<T> whose type argument is TResult .
Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Produces the set difference of two sequences by using the default equality comparer to compare values.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Except<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that are not also in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will cause those elements to be removed from the returned
sequence.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the set difference of the elements of two sequences.
Produces the set difference of two sequences by using the specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Except<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that are not also in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will cause those elements to be removed from the returned
sequence.
comparer System.Collections.Generic.IEqualityComparer<TSource>
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the set difference of the elements of two sequences.
First<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to return the first element of.
Returns
TSource
The first element in the specified sequence.
First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
The first element in the sequence that passes the test in the specified predicate function.
FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
D eclaration
public static TSource FirstOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if source is empty or if no element passes the test specified by predicate ; otherwise, the first element in
source that passes the test specified by predicate .
FirstOrDefault<TSource>(IEnumerable<TSource>)
Returns the first element of a sequence, or a default value if the sequence contains no elements.
D eclaration
public static TSource FirstOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to return the first element of.
Returns
TSource
default ( TSource ) if source is empty; otherwise, the first element in source .
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. The elements of each group are projected by using a specified function.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
GroupBy<TSource,TKey,TElement,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector,
Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
A function to create a result value from each group.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>, IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. Key values are compared by using a specified comparer, and the elements of each group
are projected by using a specified function.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
GroupBy<TSource,TKey,TElement,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector,
Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
A function to create a result value from each group.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys with.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
Groups the elements of a sequence according to a specified key selector function and projects the elements for
each group by using a specified function.
D eclaration
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in the IGrouping<TKey,TElement>.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.
Groups the elements of a sequence according to a key selector function. The keys are compared by using a
comparer and each group's elements are projected by using a specified function.
D eclaration
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult>
(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>
A function to create a result value from each group.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. The keys are compared by using a specified comparer.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult>
(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>
A function to create a result value from each group.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys with.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a sequence of objects and a key.
Groups the elements of a sequence according to a specified key selector function and compares the keys by using
a specified comparer.
D eclaration
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
GroupBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a collection of objects and a key.
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>,
Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>, IEqualityComparer<TKey>)
Correlates the elements of two sequences based on key equality and groups the results. A specified
IEqualityComparer<T> is used to compare keys.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
GroupJoin<TOuter,TInner,TKey,TResult> (this System.Collections.Generic.IEnumerable<TOuter>
outer, System.Collections.Generic.IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains elements of type TResult that are obtained by performing a grouped join on two sequences.
Correlates the elements of two sequences based on equality of keys and groups the results. The default equality
comparer is used to compare keys.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
GroupJoin<TOuter,TInner,TKey,TResult> (this System.Collections.Generic.IEnumerable<TOuter>
outer, System.Collections.Generic.IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector);
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains elements of type TResult that are obtained by performing a grouped join on two sequences.
Produces the set intersection of two sequences by using the specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Intersect<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Produces the set intersection of two sequences by using the default equality comparer to compare values.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Intersect<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Correlates the elements of two sequences based on matching keys. The default equality comparer is used to
compare keys.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> Join<TOuter,TInner,TKey,TResult>
(this System.Collections.Generic.IEnumerable<TOuter> outer,
System.Collections.Generic.IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector, Func<TOuter,TInner,TResult> resultSelector);
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that has elements of type TResult that are obtained by performing an inner join on two sequences.
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that has elements of type TResult that are obtained by performing an inner join on two sequences.
Last<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the last element of.
Returns
TSource
The value at the last position in the source sequence.
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
The last element in the sequence that passes the test in the specified predicate function.
LastOrDefault<TSource>(IEnumerable<TSource>)
Returns the last element of a sequence, or a default value if the sequence contains no elements.
D eclaration
public static TSource LastOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the last element of.
Returns
TSource
default ( TSource ) if the source sequence is empty; otherwise, the last element in the IEnumerable<T>.
LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
D eclaration
public static TSource LastOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if the sequence is empty or if no elements pass the test in the predicate function; otherwise, the last element that
passes the test in the predicate function.
LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns an Int64 that represents how many elements in a sequence satisfy a condition.
D eclaration
public static long LongCount<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements to be counted.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Int64
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
LongCount<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements to be counted.
Returns
Int64
The number of elements in the source sequence.
Max(IEnumerable<Nullable<Single>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to determine the maximum value of.
Returns
System.Nullable<System.Single>
A value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the maximum value in the sequence.
Max(IEnumerable<Int32>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to determine the maximum value of.
Returns
Int32
The maximum value in the sequence.
Max(IEnumerable<Nullable<Int64>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to determine the maximum value of.
Returns
System.Nullable<System.Int64>
A value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the maximum value in the sequence.
Max(IEnumerable<Single>)
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to determine the maximum value of.
Returns
Single
The maximum value in the sequence.
Max(IEnumerable<Nullable<Int32>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to determine the maximum value of.
Returns
System.Nullable<System.Int32>
A value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the maximum value in the sequence.
Max(IEnumerable<Decimal>)
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to determine the maximum value of.
Returns
Decimal
The maximum value in the sequence.
Max(IEnumerable<Nullable<Decimal>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to determine the maximum value of.
Returns
System.Nullable<System.Decimal>
A value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the maximum value in the sequence.
Max(IEnumerable<Int64>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to determine the maximum value of.
Returns
Int64
The maximum value in the sequence.
Max(IEnumerable<Double>)
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to determine the maximum value of.
Returns
Double
The maximum value in the sequence.
Max(IEnumerable<Nullable<Double>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to determine the maximum value of.
Returns
System.Nullable<System.Double>
A value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the maximum value in the sequence.
Max<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Invokes a transform function on each element of a generic sequence and returns the maximum resulting value.
D eclaration
public static TResult Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,TResult> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Invokes a transform function on each element of a sequence and returns the maximum Single value.
D eclaration
public static float Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,float> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
Single
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Single value.
D eclaration
public static Nullable<float> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<float>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Int64 value.
D eclaration
public static Nullable<long> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<long>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int64>
The value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Int32 value.
D eclaration
public static Nullable<int> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<int>> selector);
source, Func<TSource,Nullable<int>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int32>
The value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Double value.
D eclaration
public static Nullable<double> Max<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<double>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Invokes a transform function on each element of a sequence and returns the maximum Int64 value.
D eclaration
public static long Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,long> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Int64
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Invokes a transform function on each element of a sequence and returns the maximum Int32 value.
D eclaration
public static int Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Int32
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Invokes a transform function on each element of a sequence and returns the maximum Double value.
D eclaration
public static double Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,double> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Invokes a transform function on each element of a sequence and returns the maximum Decimal value.
D eclaration
public static decimal Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
Returns
TSource
The maximum value in the sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Decimal value.
D eclaration
public static Nullable<decimal> Max<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<decimal>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the maximum value in the
sequence.
Min(IEnumerable<Nullable<Int64>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to determine the minimum value of.
Returns
System.Nullable<System.Int64>
A value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the minimum value in the sequence.
Min(IEnumerable<Nullable<Int32>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to determine the minimum value of.
Returns
System.Nullable<System.Int32>
A value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the minimum value in the sequence.
Min(IEnumerable<Single>)
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to determine the minimum value of.
Returns
Single
The minimum value in the sequence.
Min(IEnumerable<Nullable<Single>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to determine the minimum value of.
Returns
System.Nullable<System.Single>
A value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the minimum value in the sequence.
Min(IEnumerable<Nullable<Double>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to determine the minimum value of.
Returns
System.Nullable<System.Double>
A value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the minimum value in the sequence.
Min(IEnumerable<Double>)
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to determine the minimum value of.
Returns
Double
The minimum value in the sequence.
Min(IEnumerable<Int64>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to determine the minimum value of.
Returns
Int64
The minimum value in the sequence.
Min(IEnumerable<Int32>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to determine the minimum value of.
Returns
Int32
The minimum value in the sequence.
Min(IEnumerable<Nullable<Decimal>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to determine the minimum value of.
Returns
System.Nullable<System.Decimal>
A value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the minimum value in the sequence.
Min(IEnumerable<Decimal>)
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to determine the minimum value of.
Returns
Decimal
The minimum value in the sequence.
Min<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Invokes a transform function on each element of a generic sequence and returns the minimum resulting value.
D eclaration
public static TResult Min<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,TResult> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Invokes a transform function on each element of a sequence and returns the minimum Single value.
D eclaration
public static float Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,float> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Single value.
D eclaration
public static Nullable<float> Min<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<float>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Int64 value.
D eclaration
public static Nullable<long> Min<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<long>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int64>
The value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Int32 value.
D eclaration
public static Nullable<int> Min<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<int>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int32>
The value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Double value.
D eclaration
public static Nullable<double> Min<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<double>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Invokes a transform function on each element of a sequence and returns the minimum Int64 value.
D eclaration
public static long Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,long> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Int64
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Invokes a transform function on each element of a sequence and returns the minimum Int32 value.
D eclaration
public static int Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Int32
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Invokes a transform function on each element of a sequence and returns the minimum Double value.
D eclaration
public static double Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,double> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Invokes a transform function on each element of a sequence and returns the minimum Decimal value.
D eclaration
public static decimal Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
Returns
TSource
The minimum value in the sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Decimal value.
D eclaration
public static Nullable<decimal> Min<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<decimal>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the minimum value in the sequence.
OfType<TResult>(IEnumerable)
Parameters
source IEnumerable
The IEnumerable whose elements to filter.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains elements from the input sequence of type TResult .
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
OrderByDescending<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Prepend<TSource>(IEnumerable<TSource>, TSource)
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Prepend<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, TSource element);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
element TSource
Returns
System.Collections.Generic.IEnumerable<TSource>
Range(Int32, Int32)
Parameters
start Int32
The value of the first integer in the sequence.
count Int32
The number of sequential integers to generate.
Returns
System.Collections.Generic.IEnumerable<System.Int32>
An IEnumerable in C# or IEnumerable(Of Int32) in Visual Basic that contains a range of sequential integral numbers.
Repeat<TResult>(TResult, Int32)
Parameters
element TResult
The value to be repeated.
count Int32
The number of times to repeat the value in the generated sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains a repeated value.
Reverse<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to reverse.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence whose elements correspond to those of the input sequence in reverse order.
Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,TResult>)
Projects each element of a sequence into a new form by incorporating the element's index.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,TResult> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to invoke a transform function on.
selector System.Func<TSource,System.Int32,TResult>
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source .
Select<TSource,TResult>(IEnumerable<TSource>, Func<TSource,TResult>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to invoke a transform function on.
selector System.Func<TSource,TResult>
A transform function to apply to each element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source .
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence,
and invokes a result selector function on each element therein.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
SelectMany<TSource,TCollection,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector,
Func<TSource,TCollection,TResult> resultSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
collectionSelector System.Func<TSource,System.Collections.Generic.IEnumerable<TCollection>>
A transform function to apply to each element of the input sequence.
resultSelector System.Func<TSource,TCollection,TResult>
A transform function to apply to each element of the intermediate sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function collectionSelector on each
element of source and then mapping each of those sequence elements and their corresponding source element to a result element.
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence,
and invokes a result selector function on each element therein. The index of each source element is used in the
intermediate projected form of that element.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
SelectMany<TSource,TCollection,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,int,System.Collections.Generic.IEnumerable<TCollection>>
collectionSelector, Func<TSource,TCollection,TResult> resultSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
collectionSelector System.Func<TSource,System.Int32,System.Collections.Generic.IEnumerable<TCollection>>
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
resultSelector System.Func<TSource,TCollection,TResult>
A transform function to apply to each element of the intermediate sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function collectionSelector on each
element of source and then mapping each of those sequence elements and their corresponding source element to a result element.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one
sequence.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,System.Collections.Generic.IEnumerable<TResult>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
selector System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>
selector System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>
A transform function to apply to each element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
Projects each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into one
sequence. The index of each source element is used in the projected form of that element.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
selector System.Func<TSource,System.Int32,System.Collections.Generic.IEnumerable<TResult>>
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of an input sequence.
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Determines whether two sequences are equal by comparing the elements by using the default equality comparer
for their type.
D eclaration
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource>
first, System.Collections.Generic.IEnumerable<TSource> second);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to second .
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to the first sequence.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality
comparer for their type; otherwise, false .
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Determines whether two sequences are equal by comparing their elements by using a specified
IEqualityComparer<T>.
D eclaration
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource>
first, System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to second .
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to the first sequence.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to use to compare elements.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements compare equal according to comparer ;
otherwise, false .
Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
one such element exists.
D eclaration
public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return a single element from.
predicate System.Func<TSource,System.Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies a condition.
Single<TSource>(IEnumerable<TSource>)
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the
sequence.
D eclaration
public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the single element of.
Returns
TSource
The single element of the input sequence.
SingleOrDefault<TSource>(IEnumerable<TSource>)
Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an
exception if there is more than one element in the sequence.
D eclaration
public static TSource SingleOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the single element of.
Returns
TSource
The single element of the input sequence, or default ( TSource ) if the sequence contains no elements.
SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element
exists; this method throws an exception if more than one element satisfies the condition.
D eclaration
public static TSource SingleOrDefault<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return a single element from.
predicate System.Func<TSource,System.Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition, or default ( TSource ) if no such element is found.
Skip<TSource>(IEnumerable<TSource>, Int32)
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Skip<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return elements from.
count Int32
The number of elements to skip before returning the remaining elements.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements that occur after the specified index in the input sequence.
SkipLast<TSource>(IEnumerable<TSource>, Int32)
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> SkipLast<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
count Int32
Returns
System.Collections.Generic.IEnumerable<TSource>
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> SkipWhile<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return elements from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from the input sequence starting at the first element in the linear series that does not pass
the test specified by predicate .
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
The element's index is used in the logic of the predicate function.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> SkipWhile<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return elements from.
predicate System.Func<TSource,System.Int32,System.Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from the input sequence starting at the first element in the linear series that does not pass
the test specified by predicate .
Sum(IEnumerable<Nullable<Int32>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to calculate the sum of.
Returns
System.Nullable<System.Int32>
The sum of the values in the sequence.
Sum(IEnumerable<Nullable<Single>>)
Computes the sum of a sequence of nullable Single values.
D eclaration
public static Nullable<float> Sum (this System.Collections.Generic.IEnumerable<Nullable<float>>
source);
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to calculate the sum of.
Returns
System.Nullable<System.Single>
The sum of the values in the sequence.
Sum(IEnumerable<Single>)
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to calculate the sum of.
Returns
Single
The sum of the values in the sequence.
Sum(IEnumerable<Nullable<Int64>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to calculate the sum of.
Returns
System.Nullable<System.Int64>
The sum of the values in the sequence.
Sum(IEnumerable<Nullable<Double>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to calculate the sum of.
Returns
System.Nullable<System.Double>
The sum of the values in the sequence.
Sum(IEnumerable<Double>)
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to calculate the sum of.
Returns
Double
The sum of the values in the sequence.
Sum(IEnumerable<Int64>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to calculate the sum of.
Returns
Int64
The sum of the values in the sequence.
Sum(IEnumerable<Int32>)
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to calculate the sum of.
Returns
Int32
The sum of the values in the sequence.
Sum(IEnumerable<Decimal>)
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to calculate the sum of.
Returns
Decimal
The sum of the values in the sequence.
Sum(IEnumerable<Nullable<Decimal>>)
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to calculate the sum of.
Returns
System.Nullable<System.Decimal>
The sum of the values in the sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Computes the sum of the sequence of Single values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static float Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,float> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)
Computes the sum of the sequence of nullable Single values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<float> Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<float>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)
Computes the sum of the sequence of nullable Int64 values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<long> Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<long>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int64>
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)
Computes the sum of the sequence of nullable Int32 values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<int> Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<int>> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int32>
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)
Computes the sum of the sequence of nullable Double values that are obtained by invoking a transform function
on each element of the input sequence.
D eclaration
public static Nullable<double> Sum<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<double>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)
Computes the sum of the sequence of nullable Decimal values that are obtained by invoking a transform function
on each element of the input sequence.
D eclaration
public static Nullable<decimal> Sum<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<decimal>>
selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Computes the sum of the sequence of Int64 values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static long Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,long> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Int64
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Computes the sum of the sequence of Int32 values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static int Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Int32
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Computes the sum of the sequence of Double values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static double Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,double> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The sum of the projected values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Computes the sum of the sequence of Decimal values that are obtained by invoking a transform function on each
element of the input sequence.
D eclaration
public static decimal Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The sum of the projected values.
Take<TSource>(IEnumerable<TSource>, Int32)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return elements from.
count Int32
The number of elements to return.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the specified number of elements from the start of the input sequence.
TakeLast<TSource>(IEnumerable<TSource>, Int32)
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> TakeLast<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
count Int32
Returns
System.Collections.Generic.IEnumerable<TSource>
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence to return elements from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from the input sequence that occur before the element at which the test no longer passes.
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic
of the predicate function.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return elements from.
predicate System.Func<TSource,System.Int32,System.Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains elements from the input sequence that occur before the element at which the test no longer passes.
Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
D eclaration
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this
System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)
Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
D eclaration
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this
System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)
Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
D eclaration
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
ThenByDescending<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
D eclaration
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
ToArray<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create an array from.
Returns
TSource[]
An array that contains the elements from the input sequence.
Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to specified key selector and element
selector functions.
D eclaration
public static System.Collections.Generic.Dictionary<TKey,TElement>
ToDictionary<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
System.Collections.Generic.Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence.
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Collections.Generic.Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function and
key comparer.
D eclaration
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey>
(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
ToHashSet<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
D eclaration
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
comparer System.Collections.Generic.IEqualityComparer<TSource>
Returns
System.Collections.Generic.HashSet<TSource>
ToHashSet<TSource>(IEnumerable<TSource>)
D eclaration
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
Returns
System.Collections.Generic.HashSet<TSource>
ToList<TSource>(IEnumerable<TSource>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a List<T> from.
Returns
System.Collections.Generic.List<TSource>
A List<T> that contains elements from the input sequence.
Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to specified key selector and element
selector functions.
D eclaration
public static System.Linq.ILookup<TKey,TElement> ToLookup<TSource,TKey,TElement> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
Func<TSource,TElement> elementSelector);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
ILookup<TKey,TElement>
A Lookup<TKey,TElement> that contains values of type TElement selected from the input sequence.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ILookup<TKey,TElement>
A Lookup<TKey,TElement> that contains values of type TElement selected from the input sequence.
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Linq.ILookup<TKey,TSource>
A Lookup<TKey,TElement> that contains keys and values.
Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to a specified key selector function and
key comparer.
D eclaration
public static System.Linq.ILookup<TKey,TSource> ToLookup<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Linq.ILookup<TKey,TSource>
A Lookup<TKey,TElement> that contains keys and values.
Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
Produces the set union of two sequences by using the default equality comparer.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Union<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the first set for the union.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the second set for the union.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the first set for the union.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the second set for the union.
comparer System.Collections.Generic.IEqualityComparer<TSource>
The IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.
Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate
function.
D eclaration
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to filter.
predicate System.Func<TSource,System.Int32,System.Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.
Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to filter.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult> Zip<TFirst,TSecond,TResult> (this
System.Collections.Generic.IEnumerable<TFirst> first,
System.Collections.Generic.IEnumerable<TSecond> second, Func<TFirst,TSecond,TResult>
resultSelector);
Parameters
first System.Collections.Generic.IEnumerable<TFirst>
The first sequence to merge.
second System.Collections.Generic.IEnumerable<TSecond>
The second sequence to merge.
resultSelector System.Func<TFirst,TSecond,TResult>
A function that specifies how to merge the elements from the two sequences.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains merged elements of two input sequences.
Enumerable.Aggregate
I n this Article
Overloads
Aggregate<TSource,TAccumulate,TResult>(IEnumerable<
TSource>, TAccumulate, Func<TAccumulate,TSource, Applies an accumulator function over a sequence. The
TAccumulate>, Func<TAccumulate,TResult>) specified seed value is used as the initial accumulator value,
and the specified function is used to select the result value.
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>,
TAccumulate, Func<TAccumulate,TSource,TAccumulate>) Applies an accumulator function over a sequence. The
specified seed value is used as the initial accumulator value.
Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,
TSource,TSource>) Applies an accumulator function over a sequence.
Aggregate<TSource,TAccumulate,TResult>
(IEnumerable<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>,
Func<TAccumulate,TResult>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and
the specified function is used to select the result value.
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector);
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
TResult
The type of the resulting value.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to aggregate over.
seed TAccumulate
The initial accumulator value.
func System.Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
resultSelector System.Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Exceptions
ArgumentNullException
source or func or resultSelector is null .
Examples
The following code example demonstrates how to use Aggregate to apply an accumulator function and a result
selector.
Console.WriteLine(
"The fruit with the longest name is {0}.",
longestName);
Remarks
The Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) method makes it simple to perform a
calculation over a sequence of values. This method works by calling func one time for each element in source . Each
time func is called, Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>) passes both the element from the sequence
and an aggregated value (as the first argument to func ). The value of the seed parameter is used as the initial
aggregate value. The result of func replaces the previous aggregated value. The final result of func is passed to
resultSelector to obtain the final result of Aggregate<TSource,TAccumulate,TResult>(IEnumerable<TSource>,
TAccumulate, Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>).
To simplify common aggregation operations, the standard query operators also include a general purpose count
method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>,
TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
public static TAccumulate Aggregate<TSource,TAccumulate> (this
System.Collections.Generic.IEnumerable<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> func);
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to aggregate over.
seed TAccumulate
The initial accumulator value.
func System.Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
Returns
TAccumulate
The final accumulator value.
Exceptions
ArgumentNullException
source or func is null .
Examples
The following code example demonstrates how to use Aggregate to apply an accumulator function and use a seed
value.
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
Remarks
The Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>) method makes it simple to perform a calculation over a sequence of values.
This method works by calling func one time for each element in source . Each time func is called,
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
passes both the element from the sequence and an aggregated value (as the first argument to func ). The value of the
seed parameter is used as the initial aggregate value. The result of func replaces the previous aggregated value.
Aggregate<TSource,TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
returns the final result of func .
To simplify common aggregation operations, the standard query operators also include a general purpose count
method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.
Aggregate<TSource>(IEnumerable<TSource>,
Func<TSource,TSource,TSource>)
Applies an accumulator function over a sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to aggregate over.
func System.Func<TSource,TSource,TSource>
An accumulator function to be invoked on each element.
Returns
TSource
The final accumulator value.
Exceptions
ArgumentNullException
source or func is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to reverse the order of words in a string by usingAggregate.
string sentence = "the quick brown fox jumps over the lazy dog";
Console.WriteLine(reversed);
Remarks
The Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) method makes it simple to
perform a calculation over a sequence of values. This method works by calling func one time for each element in
source except the first one. Each time func is called, Aggregate<TSource>(IEnumerable<TSource>,
Func<TSource,TSource,TSource>) passes both the element from the sequence and an aggregated value (as the first
argument to func ). The first element of source is used as the initial aggregate value. The result of func replaces the
previous aggregated value. Aggregate<TSource>(IEnumerable<TSource>, Func<TSource,TSource,TSource>) returns
the final result of func .
This overload of the Aggregatemethodisn't suitable for all casesbecause it uses the first element of source as the initial
aggregate value. You should choose another overload if the return value should include only the elements of source
that meet a certain condition. For example, this overload isn't reliableif you want to calculate the sum of the even
numbers in source . The result will be incorrect if the first element is odd instead of even.
To simplify common aggregation operations, the standard query operators also include a general purpose count
method, Count, and four numeric aggregation methods, namely Min, Max, Sum, and Average.
Enumerable.All
I n this Article
Determines whether all elements of a sequence satisfy a condition.
public static bool All<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements to apply the predicate to.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty;
otherwise, false .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use All to determine whether all the elements in a sequence satisfy a
condition. Variable allStartWithB is true if all the pet names start with "B" or if the pets array is empty.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"{0} pet names start with 'B'.",
allStartWithB ? "All" : "Not all");
}
The Boolean value that the All method returns is typically used in the predicate of a where clause ( Where clause in
Visual Basic) or a direct call to the Where method. The following example demonstrates this use of the All method.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
class Person
{
public string LastName { get; set; }
public Pet[] Pets { get; set; }
}
// Determine which people have pets that are all older than 5.
IEnumerable<string> names = from person in people
where person.Pets.All(pet => pet.Age > 5)
select person.LastName;
Remarks
Note
This method does not return all the elements of a collection. Instead, it determines whether all the elements of a
collection satisfy a condition.
The enumeration of source is stopped as soon as the result can be determined.
In Visual Basic query expression syntax, an Aggregate Into All() clause translates to an invocation of All.
Enumerable.Any
I n this Article
Overloads
Any<TSource>(IEnumerable<TSource>)
Determines whether a sequence contains any elements.
Any<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Determines whether any element of a sequence satisfies a
condition.
Any<TSource>(IEnumerable<TSource>)
Determines whether a sequence contains any elements.
public static bool Any<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to check for emptiness.
Returns
Boolean
true if the source sequence contains any elements; otherwise, false .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Any to determine whether a sequence contains any elements.
List<int> numbers = new List<int> { 1, 2 };
bool hasElements = numbers.Any();
The Boolean value that the Any<TSource>(IEnumerable<TSource>) method returns is typically used in the predicate of
a where clause ( Where clause in Visual Basic) or a direct call to the Where<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) method. The following example demonstrates this use of the Any method.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
class Person
{
public string LastName { get; set; }
public Pet[] Pets { get; set; }
}
Haas
Fakhouri
Philips
*/
}
Remarks
Note
This method does not return any one element of a collection. Instead, it determines whether the collection contains any
elements.
The enumeration of source is stopped as soon as the result can be determined.
In Visual Basic query expression syntax, an Aggregate Into Any() clause translates to an invocation of Any.
Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Determines whether any element of a sequence satisfies a condition.
public static bool Any<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to apply the predicate to.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Boolean
true if any elements in the source sequence pass the test in the specified predicate; otherwise, false .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use Any to determine whether any element in a sequence satisfies a
condition.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
public bool Vaccinated { get; set; }
}
Console.WriteLine(
"There {0} unvaccinated animals over age one.",
unvaccinated ? "are" : "are not any");
}
Remarks
Note
This method does not return any one element of a collection. Instead, it determines whether any elements of a
collection satisfy a condition.
The enumeration of source is stopped as soon as the result can be determined.
In Visual Basic query expression syntax, an Aggregate Into Any() clause translates to an invocation of Any.
Enumerable.Append
I n this Article
public static System.Collections.Generic.IEnumerable<TSource> Append<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, TSource element);
Type Parameters
TSource
Parameters
source System.Collections.Generic.IEnumerable<TSource>
element TSource
Returns
System.Collections.Generic.IEnumerable<TSource>
Enumerable.AsEnumerable
I n this Article
Returns the input typed as IEnumerable<T>.
public static System.Collections.Generic.IEnumerable<TSource> AsEnumerable<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to type as IEnumerable<T>.
Returns
System.Collections.Generic.IEnumerable<TSource>
The input sequence typed as IEnumerable<T>.
Examples
The following code example demonstrates how to use AsEnumerable<TSource>(IEnumerable<TSource>) to hide a
type's custom Where method when the standard query operator implementation is desired.
// Custom class.
class Clump<T> : List<T>
{
// Custom implementation of Where().
public IEnumerable<T> Where(Func<T, bool> predicate)
{
Console.WriteLine("In Clump's implementation of Where().");
return Enumerable.Where(this, predicate);
}
}
Remarks
The AsEnumerable<TSource>(IEnumerable<TSource>) method has no effect other than to change the compile-time
type of source from a type that implements IEnumerable<T> to IEnumerable<T> itself.
Overloads
Average(IEnumerable<Single>)
Computes the average of a sequence of Single values.
Average(IEnumerable<Nullable<Single>>)
Computes the average of a sequence of nullable Single values.
Average(IEnumerable<Nullable<Int64>>)
Computes the average of a sequence of nullable Int64 values.
Average(IEnumerable<Nullable<Int32>>)
Computes the average of a sequence of nullable Int32 values.
Average(IEnumerable<Nullable<Double>>)
Computes the average of a sequence of nullable Double
values.
Average(IEnumerable<Int64>)
Computes the average of a sequence of Int64 values.
Average(IEnumerable<Int32>)
Computes the average of a sequence of Int32 values.
Average(IEnumerable<Double>)
Computes the average of a sequence of Double values.
Average(IEnumerable<Decimal>)
Computes the average of a sequence of Decimal values.
Average(IEnumerable<Nullable<Decimal>>)
Computes the average of a sequence of nullable Decimal
values.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int32>>) Computes the average of a sequence of nullable Int32 values
that are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Single>) Computes the average of a sequence of Single values that are
obtained by invoking a transform function on each element of
the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Single>>) Computes the average of a sequence of nullable Single values
that are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int64>>) Computes the average of a sequence of nullable Int64 values
that are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Double>>) Computes the average of a sequence of nullable Double
values that are obtained by invoking a transform function on
each element of the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Decimal>>) Computes the average of a sequence of nullable Decimal
values that are obtained by invoking a transform function on
each element of the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Int64>) Computes the average of a sequence of Int64 values that are
obtained by invoking a transform function on each element of
the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Int32>) Computes the average of a sequence of Int32 values that are
obtained by invoking a transform function on each element of
the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Double>) Computes the average of a sequence of Double values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(IEnumerable<TSource>, Func<TSource,
Decimal>) Computes the average of a sequence of Decimal values that
are obtained by invoking a transform function on each
element of the input sequence.
Average(IEnumerable<Single>)
Computes the average of a sequence of Single values.
public static float Average (this System.Collections.Generic.IEnumerable<float> source);
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to calculate the average of.
Returns
Single
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IEnumerable<Int32>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Nullable<Single>>)
Computes the average of a sequence of nullable Single values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to calculate the average of.
Returns
System.Nullable<System.Single>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IEnumerable<Nullable<Int64>>) to calculate an
average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Nullable<Int64>>)
Computes the average of a sequence of nullable Int64 values.
public static Nullable<double> Average (this System.Collections.Generic.IEnumerable<Nullable<long>>
source);
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to calculate the average of.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average(IEnumerable<Nullable<Int64>>) to calculate an
average.
long?[] longs = { null, 10007L, 37L, 399846234235L };
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Nullable<Int32>>)
Computes the average of a sequence of nullable Int32 values.
public static Nullable<double> Average (this System.Collections.Generic.IEnumerable<Nullable<int>>
source);
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to calculate the average of.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average(IEnumerable<Nullable<Int64>>) to calculate an
average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
long?[] longs = { null, 10007L, 37L, 399846234235L };
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Nullable<Double>>)
Computes the average of a sequence of nullable Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to calculate the average of.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IEnumerable<Nullable<Int64>>) to calculate an
average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
If the sum of the elements is too large to represent as a Double, this method returns positive or negative infinity.
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Int64>)
Computes the average of a sequence of Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IEnumerable<Int32>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Int32>)
Computes the average of a sequence of Int32 values.
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IEnumerable<Int32>) to calculate an average.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Double>)
Computes the average of a sequence of Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to calculate the average of.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IEnumerable<Int32>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
If the sum of the elements is too large to represent as a Double, this method returns positive or negative infinity.
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Decimal>)
Computes the average of a sequence of Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to calculate the average of.
Returns
Decimal
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IEnumerable<Int32>) to calculate the average of a
sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average(IEnumerable<Nullable<Decimal>>)
Computes the average of a sequence of nullable Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to calculate the average of.
Returns
System.Nullable<System.Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average(IEnumerable<Nullable<Int64>>) to calculate an
average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
long?[] longs = { null, 10007L, 37L, 399846234235L };
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int32>>)
Computes the average of a sequence of nullable Int32 values that are obtained by invoking a transform function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int32>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Single>)
Computes the average of a sequence of Single values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int32>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Single>>)
Computes the average of a sequence of nullable Single values that are obtained by invoking a transform function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int32>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int64>>)
Computes the average of a sequence of nullable Int64 values that are obtained by invoking a transform function on
each element of the input sequence.
public static Nullable<double> Average<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,Nullable<long>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
string[] numbers = { "10007", "37", "299846234235" };
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Double>>)
Computes the average of a sequence of nullable Double values that are obtained by invoking a transform function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
string[] numbers = { "10007", "37", "299846234235" };
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Decimal>>)
Computes the average of a sequence of nullable Decimal values that are obtained by invoking a transform function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only values that are null .
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>)
Computes the average of a sequence of Int64 values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of the elements of source.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>) to calculate an average.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int32>)
Computes the average of a sequence of Int32 values that are obtained by invoking a transform function on each
element of the input sequence.
public static double Average<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int32>) to calculate an average.
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Double>)
Computes the average of a sequence of Double values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to calculate the average of.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Average<TSource>(IEnumerable<TSource>,
Func<TSource,Decimal>)
Computes the average of a sequence of Decimal values that are obtained by invoking a transform function on each
element of the input sequence.
public static decimal Average<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate an average.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
OverflowException
The sum of the elements in the sequence is larger than MaxValue.
Examples
The following code example demonstrates how to use Average<TSource>(IEnumerable<TSource>,
Func<TSource,Int64>) to calculate an average.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
In Visual Basic query expression syntax, an Aggregate Into Average() clause translates to an invocation of Average.
Enumerable.Cast
I n this Article
Casts the elements of an IEnumerable to the specified type.
public static System.Collections.Generic.IEnumerable<TResult> Cast<TResult> (this
System.Collections.IEnumerable source);
Type Parameters
TResult
The type to cast the elements of source to.
Parameters
source IEnumerable
The IEnumerable that contains the elements to be cast to type TResult .
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains each element of the source sequence cast to the specified type.
Exceptions
ArgumentNullException
source is null .
InvalidCastException
An element in the sequence cannot be cast to type TResult .
Examples
The following code example demonstrates how to use Cast<TResult>(IEnumerable) to enable the use of the standard
query operators on an ArrayList.
System.Collections.ArrayList fruits = new System.Collections.ArrayList();
fruits.Add("mango");
fruits.Add("apple");
fruits.Add("lemon");
IEnumerable<string> query =
fruits.Cast<string>().OrderBy(fruit => fruit).Select(fruit => fruit);
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The Cast<TResult>(IEnumerable) method enables the standard query operators to be invoked on non-generic
collections by supplying the necessary type information. For example, ArrayList does not implement IEnumerable<T>,
but by calling Cast<TResult>(IEnumerable) on the ArrayList object, the standard query operators can then be used to
query the sequence.
If an element cannot be cast to type TResult , this method will throw an exception. To obtain only those elements that
can be cast to type TResult , use the OfType method instead of Cast<TResult>(IEnumerable).
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
The first sequence to concatenate.
second System.Collections.Generic.IEnumerable<TSource>
The sequence to concatenate to the first sequence.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the concatenated elements of the two input sequences.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following code example demonstrates how to use Concat<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>) to concatenate two sequences.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
IEnumerable<string> query =
cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));
An alternative way of concatenating two sequences is to construct a collection, for example an array, of sequences and
then apply the SelectMany method, passing it the identity selector function. The following example demonstrates this
use of SelectMany.
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();
IEnumerable<string> query =
new[] { cats.Select(cat => cat.Name), dogs.Select(dog => dog.Name) }
.SelectMany(name => name);
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Overloads
Contains<TSource>(IEnumerable<TSource>, TSource)
Determines whether a sequence contains a specified element
by using the default equality comparer.
Contains<TSource>(IEnumerable<TSource>, TSource,
IEqualityComparer<TSource>) Determines whether a sequence contains a specified element
by using a specified IEqualityComparer<T>.
Contains<TSource>(IEnumerable<TSource>, TSource)
Determines whether a sequence contains a specified element by using the default equality comparer.
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
TSource value);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Contains<TSource>(IEnumerable<TSource>, TSource) to
determine whether an array contains a specific element.
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
Console.WriteLine(
"The array {0} contain '{1}'.",
hasMango ? "does" : "does not",
fruit);
Remarks
If the type of source implements ICollection<T>, the Contains method in that implementation is invoked to obtain
the result. Otherwise, this method determines whether source contains the specified element.
Enumeration is terminated as soon as a matching element is found.
Elements are compared to the specified value by using the default equality comparer, Default.
Contains<TSource>(IEnumerable<TSource>, TSource,
IEqualityComparer<TSource>)
Determines whether a sequence contains a specified element by using a specified IEqualityComparer<T>.
public static bool Contains<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
TSource value, System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An equality comparer to compare values.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false .
Exceptions
ArgumentNullException
source is null .
Examples
The following example shows how to implement an equality comparer that can be used in the Contains method.
public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this comparer, you can use a sequence of Product objects in the Contains method, as shown in
the following example.
Product[] fruits = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 },
new Product { Name = "lemon", Code = 12 } };
/*
This code produces the following output:
Apple? True
Kiwi? False
*/
Remarks
Enumeration is terminated as soon as a matching element is found.
If c omparer is null , the default equality comparer, Default, is used to compare elements to the specified value.
Enumerable.Count
I n this Article
Overloads
Count<TSource>(IEnumerable<TSource>)
Returns the number of elements in a sequence.
Count<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Returns a number that represents how many elements in the
specified sequence satisfy a condition.
Count<TSource>(IEnumerable<TSource>)
Returns the number of elements in a sequence.
public static int Count<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence that contains elements to be counted.
Returns
Int32
The number of elements in the input sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The number of elements in source is larger than MaxValue.
Examples
The following code example demonstrates how to use Count<TSource>(IEnumerable<TSource>) to count the
elements in an array.
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
try
{
int numberOfFruits = fruits.Count();
Console.WriteLine(
"There are {0} fruits in the collection.",
numberOfFruits);
}
catch (OverflowException)
{
Console.WriteLine("The count is too large to store as an Int32.");
Console.WriteLine("Try using the LongCount() method instead.");
}
Remarks
If the type of source implements ICollection<T>, that implementation is used to obtain the count of elements.
Otherwise, this method determines the count.
Use the LongCount method when you expect and want to allow the result to be greater than MaxValue.
In Visual Basic query expression syntax, an Aggregate Into Count() clause translates to an invocation of Count.
Count<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns a number that represents how many elements in the specified sequence satisfy a condition.
public static int Count<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence that contains elements to be tested and counted.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Int32
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
OverflowException
The number of elements in source is larger than MaxValue.
Examples
The following code example demonstrates how to use Count<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to count the elements in an array that satisfy a condition.
class Pet
{
public string Name { get; set; }
public bool Vaccinated { get; set; }
}
try
{
int numberUnvaccinated = pets.Count(p => p.Vaccinated == false);
Console.WriteLine("There are {0} unvaccinated animals.", numberUnvaccinated);
}
catch (OverflowException)
{
Console.WriteLine("The count is too large to store as an Int32.");
Console.WriteLine("Try using the LongCount() method instead.");
}
}
Remarks
If the type of source implements ICollection<T>, that implementation is used to obtain the count of elements.
Otherwise, this method determines the count.
You should use the LongCount method when you expect and want to allow the result to be greater than MaxValue.
In Visual Basic query expression syntax, an Aggregate Into Count() clause translates to an invocation of Count.
Enumerable.DefaultIfEmpty
I n this Article
Overloads
DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
Returns the elements of the specified sequence or the
specified value in a singleton collection if the sequence is
empty.
DefaultIfEmpty<TSource>(IEnumerable<TSource>)
Returns the elements of the specified sequence or the type
parameter's default value in a singleton collection if the
sequence is empty.
DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.
public static System.Collections.Generic.IEnumerable<TSource> DefaultIfEmpty<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return the specified value for if it is empty.
defaultValue TSource
The value to return if the sequence is empty.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains defaultValue if source is empty; otherwise, source .
Examples
The following code example demonstrates how to use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
method and specify a default value. The first sequence is not empty and the second sequence is empty.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
List<Pet> pets1 =
new List<Pet>{ new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
/*
This code produces the following output:
Name: Barley
Name: Boots
Name: Whiskers
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
This method can be used to produce a left outer join when it is combined with the GroupJoin) method.
DefaultIfEmpty<TSource>(IEnumerable<TSource>)
Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the
sequence is empty.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return a default value for if it is empty.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> object that contains the default value for the TSource type if source is empty; otherwise, source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code examples demonstrate how to use DefaultIfEmpty<TSource>(IEnumerable<TSource>) to provide a
default value in case the source sequence is empty.
This example uses a non-empty sequence.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Barley
Boots
Whiskers
*/
/*
This code produces the following output:
0
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
This method can be used to produce a left outer join when it is combined with the GroupJoin) method.
Enumerable.Distinct
I n this Article
Overloads
Distinct<TSource>(IEnumerable<TSource>)
Returns distinct elements from a sequence by using the
default equality comparer to compare values.
Distinct<TSource>(IEnumerable<TSource>, IEquality
Comparer<TSource>) Returns distinct elements from a sequence by using a specified
IEqualityComparer<T> to compare values.
Remarks
The result sequence is unordered.
Distinct<TSource>(IEnumerable<TSource>)
Returns distinct elements from a sequence by using the default equality comparer to compare values.
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to remove duplicate elements from.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains distinct elements from the source sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Distinct<TSource>(IEnumerable<TSource>) to return distinct
elements from a sequence of integers.
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
Console.WriteLine("Distinct ages:");
/*
This code produces the following output:
Distinct ages:
21
46
55
17
*/
If you want to return distinct elements from sequences of objects of some custom data type, you have to implement the
IEquatable<T> generic interface in the class. The following code example shows how to implement this interface in a
custom data type and provide GetHashCode and Equals methods.
After you implement this interface, you can use a sequence of Product objects in the Distinct<TSource>
(IEnumerable<TSource>) method, as shown in the following example.
Product[] products = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 },
new Product { Name = "apple", Code = 9 },
new Product { Name = "lemon", Code = 12 } };
//Exclude duplicates.
IEnumerable<Product> noduplicates =
products.Distinct();
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The default equality comparer, Default, is used to compare values of the types that implement the IEquatable<T>
generic interface. To compare a custom data type, you need to implement this interface and provide your own
GetHashCode and Equals methods for the type.
For an example that uses IEqualityComparer<T>to define a custom comparer, see Distinct<TSource>
(IEnumerable<TSource>, IEqualityComparer<TSource>).
Distinct<TSource>(IEnumerable<TSource>,
IEqualityComparer<TSource>)
Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.
public static System.Collections.Generic.IEnumerable<TSource> Distinct<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to remove duplicate elements from.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains distinct elements from the source sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following example shows how to implement an equality comparer that can be used in the Distinct method.
public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this comparer, you can use a sequence of Product objects in the Distinct method, as shown in the
following example.
//Exclude duplicates.
IEnumerable<Product> noduplicates =
products.Distinct(new ProductComparer());
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
The element at the specified position in the source sequence.
Exceptions
ArgumentNullException
source is null .
ArgumentOutOfRangeException
index is less than 0 or greater than or equal to the number of elements in source .
Examples
The following code example demonstrates how to use ElementAt to return an element at a specific position.
string[] names =
{ "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",
/*
This code produces the following sample output:
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
default ( TSource ) if the index is outside the bounds of the source sequence; otherwise, the element at the specified
position in the source sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use ElementAtOrDefault. This example uses an index that is outside
the bounds of the array.
string[] names =
{ "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
Console.WriteLine(
"The name chosen at index {0} is '{1}'.",
index,
String.IsNullOrEmpty(name) ? "<no name at this index>" : name);
/*
This code produces the following output:
Type Parameters
TResult
The type to assign to the type parameter of the returned generic IEnumerable<T>.
Returns
System.Collections.Generic.IEnumerable<TResult>
An empty IEnumerable<T> whose type argument is TResult .
Examples
The following code example demonstrates how to use Empty<TResult>() to generate an empty IEnumerable<T>.
IEnumerable<decimal> empty = Enumerable.Empty<decimal>();
The following code example demonstrates a possible application of the Empty<TResult>() method. The Aggregate
method is applied to a collection of string arrays. The elements of each array in the collection are added to the resulting
IEnumerable<T> only if that array contains four or more elements. Empty is used to generate the seed value for
Aggregate because if no array in the collection has four or more elements, only the empty sequence is returned.
string[] names1 = { "Hartono, Tommy" };
string[] names2 = { "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
string[] names3 = { "Solanki, Ajay", "Hoeing, Helge",
"Andersen, Henriette Thaulow",
"Potra, Cristina", "Iallo, Lucio" };
List<string[]> namesList =
new List<string[]> { names1, names2, names3 };
/*
This code produces the following output:
Adams, Terry
Andersen, Henriette Thaulow
Hedlund, Magnus
Ito, Shu
Solanki, Ajay
Hoeing, Helge
Potra, Cristina
Iallo, Lucio
*/
Remarks
The Empty<TResult>() method caches an empty sequence of type TResult . When the object it returns is enumerated,
it yields no elements.
In some cases, this method is useful for passing an empty sequence to a user-defined method that takes an
IEnumerable<T>. It can also be used to generate a neutral element for methods such as Union. See the Example section
for an example of this use of Empty<TResult>().
Enumerable.Except
I n this Article
Overloads
Except<TSource>(IEnumerable<TSource>, IEnumerable<
TSource>) Produces the set difference of two sequences by using the
default equality comparer to compare values.
Except<TSource>(IEnumerable<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) Produces the set difference of two sequences by using the
specified IEqualityComparer<T> to compare values.
Except<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>)
Produces the set difference of two sequences by using the default equality comparer to compare values.
public static System.Collections.Generic.IEnumerable<TSource> Except<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that are not also in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will cause those elements to be removed
from the returned sequence.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the set difference of the elements of two sequences.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following code example demonstrates how to use the Except<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>) method to compare two sequences of numbers and return elements that appear only in the
first sequence.
double[] numbers1 = { 2.0, 2.0, 2.1, 2.2, 2.3, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.2 };
/*
This code produces the following output:
2
2.1
2.3
2.4
2.5
*/
If you want to compare sequences of objects of some custom data type, you have to implement the
IEqualityComparer<T> generic interface in a helperclass. The following code example shows how to implement this
interface in a custom data type and provide GetHashCode and Equals methods.
public class ProductA
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this interface, you can use sequences of ProductA objects in the Except<TSource>
(IEnumerable<TSource>, IEnumerable<TSource>) method, as shown in the following example.
ProductA[] fruits1 = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "orange", Code = 4 },
new ProductA { Name = "lemon", Code = 12 } };
IEnumerable<ProductA> except =
fruits1.Except(fruits2);
/*
This code produces the following output:
orange 4
lemon 12
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The set difference of two sets is defined as the members of the first set that do not appear in the second set.
Note
This method returns those elements in first that do not appear in second . It does not also return those elements in
second that do not appear in first .
The default equality comparer, Default, is used to compare values of the types that implement the
IEqualityComparer<T> generic interface. To compare a custom data type, you need to implement this interface and
provide your own GetHashCode and Equals methods for the type.
Except<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
Produces the set difference of two sequences by using the specified IEqualityComparer<T> to compare values.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that are not also in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will cause those elements to be removed
from the returned sequence.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the set difference of the elements of two sequences.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following example shows how to implement an equality comparer that can be used in the Except method.
public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this comparer, you can use sequences of Product objects in the Except method, as shown in the
following example.
Product[] fruits1 = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 },
new Product { Name = "lemon", Code = 12 } };
IEnumerable<Product> except =
fruits1.Except(fruits2, new ProductComparer());
/*
This code produces the following output:
orange 4
lemon 12
*/
Remarks
If c omparer is null , the default equality comparer, Default, is used to compare values.
Enumerable.First
I n this Article
Overloads
First<TSource>(IEnumerable<TSource>)
Returns the first element of a sequence.
First<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Returns the first element in a sequence that satisfies a
specified condition.
First<TSource>(IEnumerable<TSource>)
Returns the first element of a sequence.
public static TSource First<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to return the first element of.
Returns
TSource
The first element in the specified sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
The source sequence is empty.
Examples
The following code example demonstrates how to use First<TSource>(IEnumerable<TSource>) to return the first
element of an array.
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 435, 67, 12, 19 };
Console.WriteLine(first);
/*
This code produces the following output:
9
*/
Remarks
The First<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. To instead
return a default value when the source sequence is empty, use the FirstOrDefault method.
First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the first element in a sequence that satisfies a specified condition.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
The first element in the sequence that passes the test in the specified predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
No element satisfies the condition in predicate .
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use First<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to return the first element of an array that satisfies a condition.
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 435, 67, 12, 19 };
Console.WriteLine(first);
/*
This code produces the following output:
92
*/
Remarks
The First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if no matching
element is found in source . To instead return a default value when no matching element is found, use the
FirstOrDefault method.
Enumerable.FirstOrDefault
I n this Article
Overloads
FirstOrDefault<TSource>(IEnumerable<TSource>, Func<
TSource,Boolean>) Returns the first element of the sequence that satisfies a
condition or a default value if no such element is found.
FirstOrDefault<TSource>(IEnumerable<TSource>)
Returns the first element of a sequence, or a default value if
the sequence contains no elements.
FirstOrDefault<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
public static TSource FirstOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if sourceis empty or if no element passes the test specified by predicate ; otherwise, the first
element in source that passes the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use FirstOrDefault<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) by passing in a predicate. In the second call to the method, there is no element in the array
that satisfies the condition.
string[] names = { "Hartono, Tommy", "Adams, Terry",
"Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
Console.WriteLine(
"There is {0} name longer than 30 characters.",
string.IsNullOrEmpty(firstVeryLongName) ? "not a" : "a");
/*
This code produces the following output:
Remarks
The default value for reference and nullable types is null .
FirstOrDefault<TSource>(IEnumerable<TSource>)
Returns the first element of a sequence, or a default value if the sequence contains no elements.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to return the first element of.
Returns
TSource
default ( TSource ) if source is empty; otherwise, the first element in source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use FirstOrDefault<TSource>(IEnumerable<TSource>) on an empty
array.
int[] numbers = { };
int first = numbers.FirstOrDefault();
Console.WriteLine(first);
/*
This code produces the following output:
0
*/
Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no
elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use
the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method to specify the default value that you want to
use if the collection is empty. Then, call First<TSource>(IEnumerable<TSource>) to obtain the first element. The
following code example uses both techniques to obtain a default value of 1 if a collection of numeric months is empty.
Because the default value for an integer is 0, which does not correspond to any month, the default value must be
specified as 1 instead. The first result variable is checked for the unwanted default value after the query has finished
executing. The second result variable is obtained by using DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource)
to specify a default value of 1.
List<int> months = new List<int> { };
/*
This code produces the following output:
Remarks
The default value for reference and nullable types is null .
The FirstOrDefault method does not provide a way to specify a default value. If you want to specify a default value
other than default(TSource) , use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as
described in the Example section.
Enumerable.GroupBy
I n this Article
Overloads
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Groups the elements of a sequence according to a specified
Func<TKey,IEnumerable<TElement>,TResult>) key selector function and creates a result value from each
group and its key. The elements of each group are projected
by using a specified function.
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Groups the elements of a sequence according to a specified
Func<TKey,IEnumerable<TElement>,TResult>, IEquality key selector function and creates a result value from each
Comparer<TKey>) group and its key. Key values are compared by using a
specified comparer, and the elements of each group are
projected by using a specified function.
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) Groups the elements of a sequence according to a specified
key selector function and projects the elements for each group
by using a specified function.
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>, IEquality Groups the elements of a sequence according to a key selector
Comparer<TKey>) function. The keys are compared by using a comparer and
each group's elements are projected by using a specified
function.
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>, Groups the elements of a sequence according to a specified
TResult>) key selector function and creates a result value from each
group and its key.
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>, Groups the elements of a sequence according to a specified
TResult>, IEqualityComparer<TKey>) key selector function and creates a result value from each
group and its key. The keys are compared by using a specified
comparer.
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>) Groups the elements of a sequence according to a specified
key selector function.
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>, IEqualityComparer<TKey>) Groups the elements of a sequence according to a specified
key selector function and compares the keys by using a
specified comparer.
GroupBy<TSource,TKey,TElement,TResult>
(IEnumerable<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>,
Func<TKey,IEnumerable<TElement>,TResult>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. The elements of each group are projected by using a specified function.
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult>
(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
Func<TSource,TElement> elementSelector,
Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in each IGrouping<TKey,TElement>.
TResult
The type of the result value returned by resultSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
A function to create a result value from each group.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey,TElement,TResult>
(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>,
Func<TKey,IEnumerable<TElement>,TResult>) to group the projected elements of a sequence and then project a
sequence of results of type TResult .
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Remarks
In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation
of GroupBy.
GroupBy<TSource,TKey,TElement,TResult>
(IEnumerable<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>,
Func<TKey,IEnumerable<TElement>,TResult>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. Key values are compared by using a specified comparer, and the elements of each group are
projected by using a specified function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in each IGrouping<TKey,TElement>.
TResult
The type of the result value returned by resultSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
A function to create a result value from each group.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys with.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>)
Groups the elements of a sequence according to a specified key selector function and projects the elements for each
group by using a specified function.
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in the IGrouping<TKey,TElement>.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in the IGrouping<TKey,TElement>.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) to group the elements of a sequence.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
8
Barley
4
Boots
Daisy
1
Whiskers
*/
In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation
of GroupBy. The translation of the query expression in the following example is equivalent to the query in the example
above.
Note
In a Visual C# or Visual Basic query expression, the element and key selection expressions occur in the reverse order
from their argument positions in a call to the GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) method.
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer
and each group's elements are projected by using a specified function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in the IGrouping<TKey,TElement>.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
elementSelector System.Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Note
If comparer is null , the default equality comparer Default is used to compare keys.
If two keys are considered equal according to comparer , the first key is chosen as the key for that grouping.
In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation
of GroupBy. For more information and usage examples, see group clause and Group By Clause.
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>,
Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TResult
The type of the result value returned by resultSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>
A function to create a result value from each group.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) to group the elements of a sequence and project a
sequence of results of type TResult .
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Remarks
In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation
of GroupBy.
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>,
Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. The keys are compared by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TResult
The type of the result value returned by resultSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>
A function to create a result value from each group.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys with.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>)
Groups the elements of a sequence according to a specified key selector function.
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
GroupBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a sequence of objects and a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Note
GroupBy<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and compares the keys by using a
specified comparer.
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
GroupBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose elements to group.
keySelector System.Func<TSource,TKey>
A function to extract the key for each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>>
An IEnumerable<>> in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a collection of objects and a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Note
If two keys are considered equal according to comparer , the first key is chosen as the key for that grouping.
In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation
of GroupBy. For more information and usage examples, see group clause and Group By Clause.
Enumerable.GroupJoin
I n this Article
Overloads
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<
TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func< Correlates the elements of two sequences based on key
TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>, equality and groups the results. A specified
IEqualityComparer<TKey>) IEqualityComparer<T> is used to compare keys.
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<
TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func< Correlates the elements of two sequences based on equality of
TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>) keys and groups the results. The default equality comparer is
used to compare keys.
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,IEnumerable<TInner>,TResult>,
IEqualityComparer<TKey>)
Correlates the elements of two sequences based on key equality and groups the results. A specified
IEqualityComparer<T> is used to compare keys.
public static System.Collections.Generic.IEnumerable<TResult> GroupJoin<TOuter,TInner,TKey,TResult>
(this System.Collections.Generic.IEnumerable<TOuter> outer,
System.Collections.Generic.IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains elements of type TResult that are obtained by performing a grouped join on two
sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
If c omparer is null , the default equality comparer, Default, is used to hash and compare keys.
GroupJoin produces hierarchical results, which means that elements from outer are paired with collections of
matching elements from inner . GroupJoin enables you to base your results on a whole set of matches for each
element of outer .
Note
If there are no correlated elements in inner for a given element of outer , the sequence of matches for that element
will be empty but will still appear in the results.
The resultSelector function is called only one time for each outer element together with a collection of all the
inner elements that match the outer element. This differs from the Join method in which the result selector function
is invoked on pairs that contain one element from outer and one element from inner .
GroupJoin preserves the order of the elements of outer , and for each element of outer , the order of the matching
elements from inner .
GroupJoin has no direct equivalent in traditional relational database terms. However, this method does implement a
superset of inner joins and left outer joins. Both of these operations can be written in terms of a grouped join. See Join
Operations.
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,IEnumerable<TInner>,TResult>)
Correlates the elements of two sequences based on equality of keys and groups the results. The default equality
comparer is used to compare keys.
public static System.Collections.Generic.IEnumerable<TResult> GroupJoin<TOuter,TInner,TKey,TResult>
(this System.Collections.Generic.IEnumerable<TOuter> outer,
System.Collections.Generic.IEnumerable<TInner> inner, Func<TOuter,TKey> outerKeySelector,
Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector);
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains elements of type TResult that are obtained by performing a grouped join on two
sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Examples
The following code example demonstrates how to use GroupJoin<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>) to
perform a grouped join on two sequences.
class Person
{
public string Name { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
/*
This code produces the following output:
Hedlund, Magnus:
Daisy
Adams, Terry:
Barley
Boots
Weiss, Charlotte:
Whiskers
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The default equality comparer, Default, is used to hash and compare keys.
GroupJoin produces hierarchical results, which means that elements from outer are paired with collections of
matching elements from inner . GroupJoin enables you to base your results on a whole set of matches for each
element of outer .
Note
If there are no correlated elements in inner for a given element of outer , the sequence of matches for that element
will be empty but will still appear in the results.
The resultSelector function is called only one time for each outer element together with a collection of all the
inner elements that match the outer element. This differs from the Join method, in which the result selector function
is invoked on pairs that contain one element from outer and one element from inner .
GroupJoin preserves the order of the elements of outer , and for each element of outer , the order of the matching
elements from inner .
GroupJoin has no direct equivalent in traditional relational database terms. However, this method does implement a
superset of inner joins and left outer joins. Both of these operations can be written in terms of a grouped join. See Join
Operations.
In query expression syntax, a join … into (Visual C#) or Group Join (Visual Basic) clause translates to an invocation
of GroupJoin.
Enumerable.Intersect
I n this Article
Overloads
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) Produces the set intersection of two sequences by using the
specified IEqualityComparer<T> to compare values.
Intersect<TSource>(IEnumerable<TSource>, IEnumerable<
TSource>) Produces the set intersection of two sequences by using the
default equality comparer to compare values.
Intersect<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
Produces the set intersection of two sequences by using the specified IEqualityComparer<T> to compare values.
public static System.Collections.Generic.IEnumerable<TSource> Intersect<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following example shows how to implement an equality comparer that can be used in the Intersect method.
public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this comparer, you can use sequences of Product objects in the Intersect method, as shown in the
following example.
Product[] store1 = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 } };
IEnumerable<Product> duplicates =
store1.Intersect(store2, new ProductComparer());
/*
This code produces the following output:
apple 9
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no
other elements.
When the object returned by this method is enumerated, Intersect yields distinct elements occurring in both sequences
in the order in which they appear in first .
If comparer is null , the default equality comparer, Default, is used to compare values.
Intersect<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>)
Produces the set intersection of two sequences by using the default equality comparer to compare values.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in second will be returned.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following code example demonstrates how to use Intersect<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>) to return the elements that appear in each of two sequences of integers.
/*
This code produces the following output:
26
30
*/
If you want to compare sequences of objects of a custom data type, you have to implement the IEqualityComparer<T>
generic interface in a helper class. The following code example shows how to implement this interface in a custom data
type and provide GetHashCode and Equals methods.
After you implement this interface, you can use sequences of ProductA objects in the Intersect method, as shown in
the following example.
ProductA[] store1 = { new ProductA { Name = "apple", Code = 9 },
new ProductA { Name = "orange", Code = 4 } };
IEnumerable<ProductA> duplicates =
store1.Intersect(store2, new ProductComparer());
/*
This code produces the following output:
apple 9
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The intersection of two sets A and B is defined as the set that contains all the elements of A that also appear in B, but no
other elements.
When the object returned by this method is enumerated, Intersect yields distinct elements occurring in both sequences
in the order in which they appear in first .
The default equality comparer, Default, is used to compare values of the types that implement the
IEqualityComparer<T> generic interface. To compare a custom data type, you need to implement this interface and
provide your own GetHashCode and Equals methods for the type.
Enumerable.Join
I n this Article
Overloads
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner, Correlates the elements of two sequences based on matching
TKey>, Func<TOuter,TInner,TResult>) keys. The default equality comparer is used to compare keys.
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner, Correlates the elements of two sequences based on matching
TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer< keys. A specified IEqualityComparer<T> is used to compare
TKey>) keys.
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,TInner,TResult>)
Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare
keys.
public static System.Collections.Generic.IEnumerable<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Collections.Generic.IEnumerable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner>
inner, Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector);
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that has elements of type TResult that are obtained by performing an inner join on two
sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Examples
The following code example demonstrates how to use Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>, Func<TOuter,TInner,TResult>) to perform an inner
join of two sequences based on a common key.
class Person
{
public string Name { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
/*
This code produces the following output:
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The default equality comparer, Default, is used to hash and compare keys.
A join refers to the operation of correlating the elements of two sources of information based on a common key. Join
brings the two information sources and the keys by which they are matched together in one method call. This differs
from the use of SelectMany , which requires more than one method call to perform the same operation.
Join preserves the order of the elements of outer , and for each of these elements, the order of the matching elements
of inner .
In query expression syntax, a join (Visual C#) or Join (Visual Basic) clause translates to an invocation of Join.
In relational database terms, the Join method implements an inner equijoin. 'Inner' means that only elements that have
a match in the other sequence are included in the results. An 'equijoin' is a join in which the keys are compared for
equality. A left outer join operation has no dedicated standard query operator, but can be performed by using the
GroupJoin method. See Join Operations.
Join<TOuter,TInner,TKey,TResult>(IEnumerable<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)
Correlates the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to
compare keys.
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer System.Collections.Generic.IEnumerable<TOuter>
The first sequence to join.
inner System.Collections.Generic.IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector System.Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector System.Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector System.Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that has elements of type TResult that are obtained by performing an inner join on two
sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
If c omparer is null , the default equality comparer, Default, is used to hash and compare keys.
A join refers to the operation of correlating the elements of two sources of information based on a common key. Join
brings the two information sources and the keys by which they are matched together in one method call. This differs
from the use of SelectMany , which requires more than one method call to perform the same operation.
Join preserves the order of the elements of outer , and for each of these elements, the order of the matching elements
of inner .
In relational database terms, the Join method implements an inner equijoin. 'Inner' means that only elements that have
a match in the other sequence are included in the results. An 'equijoin' is a join in which the keys are compared for
equality. A left outer join operation has no dedicated standard query operator, but can be performed by using the
GroupJoin method. See Join Operations.
Enumerable.Last
I n this Article
Overloads
Last<TSource>(IEnumerable<TSource>)
Returns the last element of a sequence.
Last<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Returns the last element of a sequence that satisfies a
specified condition.
Last<TSource>(IEnumerable<TSource>)
Returns the last element of a sequence.
public static TSource Last<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the last element of.
Returns
TSource
The value at the last position in the source sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
The source sequence is empty.
Examples
The following code example demonstrates how to use Last<TSource>(IEnumerable<TSource>) to return the last
element of an array.
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
Console.WriteLine(last);
/*
This code produces the following output:
19
*/
Remarks
The Last<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. To instead
return a default value when the source sequence is empty, use the LastOrDefault method.
Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Returns the last element of a sequence that satisfies a specified condition.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
The last element in the sequence that passes the test in the specified predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
No element satisfies the condition in predicate .
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use Last<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to return the last element of an array that satisfies a condition.
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
Console.WriteLine(last);
/*
This code produces the following output:
87
*/
Remarks
The Last<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if no matching
element is found in source . To instead return a default value when no matching element is found, use the
LastOrDefault method.
Enumerable.LastOrDefault
I n this Article
Overloads
LastOrDefault<TSource>(IEnumerable<TSource>)
Returns the last element of a sequence, or a default value if
the sequence contains no elements.
LastOrDefault<TSource>(IEnumerable<TSource>, Func<
TSource,Boolean>) Returns the last element of a sequence that satisfies a
condition or a default value if no such element is found.
LastOrDefault<TSource>(IEnumerable<TSource>)
Returns the last element of a sequence, or a default value if the sequence contains no elements.
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the last element of.
Returns
TSource
default ( TSource ) if the source sequence is empty; otherwise, the last element in the IEnumerable<T>.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use LastOrDefault<TSource>(IEnumerable<TSource>) on an empty
array.
string[] fruits = { };
string last = fruits.LastOrDefault();
Console.WriteLine(
String.IsNullOrEmpty(last) ? "<string is null or empty>" : last);
/*
This code produces the following output:
Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no
elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use
the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method to specify the default value that you want to
use if the collection is empty. Then, call Last<TSource>(IEnumerable<TSource>) to obtain the last element. The
following code example uses both techniques to obtain a default value of 1 if a collection of numeric days of the month
is empty. Because the default value for an integer is 0, which does not correspond to any day of the month, the default
value must be specified as 1 instead. The first result variable is checked for the unwanted default value after the query
has finished executing. The second result variable is obtained by using DefaultIfEmpty<TSource>
(IEnumerable<TSource>, TSource) to specify a default value of 1.
/*
This code produces the following output:
Remarks
The default value for reference and nullable types is null .
The LastOrDefault method does not provide a way to specify a default value. If you want to specify a default value other
than default(TSource) , use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as described in
the Example section.
LastOrDefault<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return an element from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if the sequence is empty or if no elements pass the test in the predicate function; otherwise, the last
element that passes the test in the predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use LastOrDefault<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) by passing in a predicate. In the second call to the method, there is no element in the
sequence that satisfies the condition.
double[] numbers = { 49.6, 52.3, 51.0, 49.4, 50.2, 48.3 };
Console.WriteLine(
"The last number that rounds to 40 is {0}.",
last40 == 0.0 ? "<DOES NOT EXIST>" : last40.ToString());
/*
This code produces the following output:
Remarks
The default value for reference and nullable types is null .
Enumerable.LongCount
I n this Article
Overloads
LongCount<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Returns an Int64 that represents how many elements in a
sequence satisfy a condition.
LongCount<TSource>(IEnumerable<TSource>)
Returns an Int64 that represents the total number of elements
in a sequence.
LongCount<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns an Int64 that represents how many elements in a sequence satisfy a condition.
public static long LongCount<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements to be counted.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
Int64
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
OverflowException
The number of matching elements exceeds MaxValue.
Examples
The following code example demonstrates how to use LongCount<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to count the elements in an array that satisfy a condition.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
Use this method rather than Count when you expect the result to be greater than MaxValue.
In Visual Basic query expression syntax, an Aggregate Into LongCount() clause translates to an invocation of
LongCount.
LongCount<TSource>(IEnumerable<TSource>)
Returns an Int64 that represents the total number of elements in a sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements to be counted.
Returns
Int64
The number of elements in the source sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The number of elements exceeds MaxValue.
Examples
The following code example demonstrates how to use LongCount<TSource>(IEnumerable<TSource>) to count the
elements in an array.
/*
This code produces the following output:
Remarks
Use this method rather than Count when you expect the result to be greater than MaxValue.
In Visual Basic query expression syntax, an Aggregate Into LongCount() clause translates to an invocation of
LongCount.
Enumerable.Max
I n this Article
Overloads
Max(IEnumerable<Nullable<Single>>)
Returns the maximum value in a sequence of nullable Single
values.
Max(IEnumerable<Int32>)
Returns the maximum value in a sequence of Int32 values.
Max(IEnumerable<Nullable<Int64>>)
Returns the maximum value in a sequence of nullable Int64
values.
Max(IEnumerable<Single>)
Returns the maximum value in a sequence of Single values.
Max(IEnumerable<Nullable<Int32>>)
Returns the maximum value in a sequence of nullable Int32
values.
Max(IEnumerable<Decimal>)
Returns the maximum value in a sequence of Decimal values.
Max(IEnumerable<Nullable<Decimal>>)
Returns the maximum value in a sequence of nullable Decimal
values.
Max(IEnumerable<Int64>)
Returns the maximum value in a sequence of Int64 values.
Max(IEnumerable<Double>)
Returns the maximum value in a sequence of Double values.
Max(IEnumerable<Nullable<Double>>)
Returns the maximum value in a sequence of nullable Double
values.
Max<TSource,TResult>(IEnumerable<TSource>, Func<
TSource,TResult>) Invokes a transform function on each element of a generic
sequence and returns the maximum resulting value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Single>) Invokes a transform function on each element of a sequence
and returns the maximum Single value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Single>>) Invokes a transform function on each element of a sequence
and returns the maximum nullable Single value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int64>>) Invokes a transform function on each element of a sequence
and returns the maximum nullable Int64 value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int32>>) Invokes a transform function on each element of a sequence
and returns the maximum nullable Int32 value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Double>>) Invokes a transform function on each element of a sequence
and returns the maximum nullable Double value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Int64>) Invokes a transform function on each element of a sequence
and returns the maximum Int64 value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Int32>) Invokes a transform function on each element of a sequence
and returns the maximum Int32 value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Double>) Invokes a transform function on each element of a sequence
and returns the maximum Double value.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Decimal>) Invokes a transform function on each element of a sequence
and returns the maximum Decimal value.
Max<TSource>(IEnumerable<TSource>)
Returns the maximum value in a generic sequence.
Max<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Decimal>>) Invokes a transform function on each element of a sequence
and returns the maximum nullable Decimal value.
Max(IEnumerable<Nullable<Single>>)
Returns the maximum value in a sequence of nullable Single values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to determine the maximum value of.
Returns
System.Nullable<System.Single>
A value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the maximum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the
maximum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Nullable<Single>>) method uses the Single implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Int32>)
Returns the maximum value in a sequence of Int32 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to determine the maximum value of.
Returns
Int32
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Int32>) method uses the Int32 implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Nullable<Int64>>)
Returns the maximum value in a sequence of nullable Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to determine the maximum value of.
Returns
System.Nullable<System.Int64>
A value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the maximum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the
maximum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Nullable<Int64>>) method uses the Int64 implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Single>)
Returns the maximum value in a sequence of Single values.
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to determine the maximum value of.
Returns
Single
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Single>) method uses the Single implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Nullable<Int32>>)
Returns the maximum value in a sequence of nullable Int32 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to determine the maximum value of.
Returns
System.Nullable<System.Int32>
A value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the maximum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the
maximum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Nullable<Int32>>) method uses the Int32 implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Decimal>)
Returns the maximum value in a sequence of Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to determine the maximum value of.
Returns
Decimal
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Decimal>) method uses the Decimal implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Nullable<Decimal>>)
Returns the maximum value in a sequence of nullable Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to determine the maximum value of.
Returns
System.Nullable<System.Decimal>
A value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the maximum value in
the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the
maximum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
double?[] doubles = { null, 1.5E+104, 9E+103, -2E+103 };
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Nullable<Decimal>>) method uses the Decimal implementation of IComparable<T> to
compare values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Int64>)
Returns the maximum value in a sequence of Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to determine the maximum value of.
Returns
Int64
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a
sequence.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Int64>) method uses the Int64 implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Double>)
Returns the maximum value in a sequence of Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to determine the maximum value of.
Returns
Double
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max(IEnumerable<Int64>) to determine the maximum value in a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<long> longs = new List<long> { 4294967296L, 466855135L, 81125L };
/*
This code produces the following output:
Remarks
The Max(IEnumerable<Double>) method uses the Double implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max(IEnumerable<Nullable<Double>>)
Returns the maximum value in a sequence of nullable Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to determine the maximum value of.
Returns
System.Nullable<System.Double>
A value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the maximum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max(IEnumerable<Nullable<Double>>) to determine the
maximum value in a sequence.
/*
This code produces the following output:
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,TResult>)
Invokes a transform function on each element of a generic sequence and returns the maximum resulting value.
public static TResult Max<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,TResult> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by selector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
If type TResult implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if
type TResult implements IComparable, that implementation is used to compare values.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Invokes a transform function on each element of a sequence and returns the maximum Single value.
public static float Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,float> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Single>) method uses the Single implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Single.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Single>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Single value.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the maximum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Max<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int64>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Int64 value.
public static Nullable<long> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<long>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int64>
The value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the maximum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) method uses the Int64 implementation
of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Int64> in C# or Nullable(Of Int64) in Visual Basic.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int32>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Int32 value.
public static Nullable<int> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<int>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int32>
The value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the maximum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) method uses the Int32 implementation
of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Int32> in C# or Nullable(Of Int32) in Visual Basic.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Double>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Double value.
public static Nullable<double> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<double>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the maximum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) method uses the Double
implementation of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Double> in C# or Nullable(Of Double) in Visual
Basic.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Invokes a transform function on each element of a sequence and returns the maximum Int64 value.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Int64
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) method uses the Int64 implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Int64.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Invokes a transform function on each element of a sequence and returns the maximum Int32 value.
public static int Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Int32
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) method uses the Int32 implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Int32.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Invokes a transform function on each element of a sequence and returns the maximum Double value.
public static double Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,double> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Double>) method uses the Double implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Double.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Invokes a transform function on each element of a sequence and returns the maximum Decimal value.
public static decimal Max<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) method uses the Decimal implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Decimal.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>)
Returns the maximum value in a generic sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
Returns
TSource
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>) to determine the
maximum value in a sequence of IComparable<T> objects.
/// <summary>
/// This class implements IComparable to be able to
/// compare one Pet to another Pet.
/// </summary>
class Pet : IComparable<Pet>
{
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// Compares this Pet to another Pet by
/// summing each Pet's age and name length.
/// </summary>
/// <param name="other">The Pet to compare this Pet to.</param>
/// <returns>-1 if this Pet is 'less' than the other Pet,
/// 0 if they are equal,
/// or 1 if this Pet is 'greater' than the other Pet.</returns>
int IComparable<Pet>.CompareTo(Pet other)
{
int sumOther = other.Age + other.Name.Length;
int sumThis = this.Age + this.Name.Length;
Console.WriteLine(
"The 'maximum' animal is {0}.",
max.Name);
}
/*
This code produces the following output:
Remarks
If type TSource implements IComparable<T>, the Max<TSource>(IEnumerable<TSource>) method uses that
implementation to compare values. Otherwise, if type TSource implements IComparable, that implementation is used
to compare values.
If TSource is a reference type and the source sequence is empty or contains only values that are null , this method
returns null .
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Max<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Decimal>>)
Invokes a transform function on each element of a sequence and returns the maximum nullable Decimal value.
public static Nullable<decimal> Max<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<decimal>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the maximum value of.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the maximum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the maximum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
The Max<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) method uses the Decimal
implementation of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Decimal> in C# or Nullable(Of Decimal) in Visual
Basic.
In Visual Basic query expression syntax, an Aggregate Into Max() clause translates to an invocation of Max.
Enumerable.Min
I n this Article
Overloads
Min(IEnumerable<Nullable<Int64>>)
Returns the minimum value in a sequence of nullable Int64
values.
Min(IEnumerable<Nullable<Int32>>)
Returns the minimum value in a sequence of nullable Int32
values.
Min(IEnumerable<Single>)
Returns the minimum value in a sequence of Single values.
Min(IEnumerable<Nullable<Single>>)
Returns the minimum value in a sequence of nullable Single
values.
Min(IEnumerable<Nullable<Double>>)
Returns the minimum value in a sequence of nullable Double
values.
Min(IEnumerable<Double>)
Returns the minimum value in a sequence of Double values.
Min(IEnumerable<Int64>)
Returns the minimum value in a sequence of Int64 values.
Min(IEnumerable<Int32>)
Returns the minimum value in a sequence of Int32 values.
Min(IEnumerable<Nullable<Decimal>>)
Returns the minimum value in a sequence of nullable Decimal
values.
Min(IEnumerable<Decimal>)
Returns the minimum value in a sequence of Decimal values.
Min<TSource,TResult>(IEnumerable<TSource>, Func<
TSource,TResult>) Invokes a transform function on each element of a generic
sequence and returns the minimum resulting value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Single>) Invokes a transform function on each element of a sequence
and returns the minimum Single value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Single>>) Invokes a transform function on each element of a sequence
and returns the minimum nullable Single value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int64>>) Invokes a transform function on each element of a sequence
and returns the minimum nullable Int64 value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int32>>) Invokes a transform function on each element of a sequence
and returns the minimum nullable Int32 value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Double>>) Invokes a transform function on each element of a sequence
and returns the minimum nullable Double value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Int64>) Invokes a transform function on each element of a sequence
and returns the minimum Int64 value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Int32>) Invokes a transform function on each element of a sequence
and returns the minimum Int32 value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Double>) Invokes a transform function on each element of a sequence
and returns the minimum Double value.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Decimal>) Invokes a transform function on each element of a sequence
and returns the minimum Decimal value.
Min<TSource>(IEnumerable<TSource>)
Returns the minimum value in a generic sequence.
Min<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Decimal>>) Invokes a transform function on each element of a sequence
and returns the minimum nullable Decimal value.
Min(IEnumerable<Nullable<Int64>>)
Returns the minimum value in a sequence of nullable Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to determine the minimum value of.
Returns
System.Nullable<System.Int64>
A value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the minimum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the
minimum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Nullable<Int64>>) method uses the Int64 implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Nullable<Int32>>)
Returns the minimum value in a sequence of nullable Int32 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to determine the minimum value of.
Returns
System.Nullable<System.Int32>
A value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the minimum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the
minimum value in a sequence.
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Nullable<Int32>>) method uses the Int32 implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Single>)
Returns the minimum value in a sequence of Single values.
public static float Min (this System.Collections.Generic.IEnumerable<float> source);
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to determine the minimum value of.
Returns
Single
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in
a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Single>) method uses the Single implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Nullable<Single>>)
Returns the minimum value in a sequence of nullable Single values.
public static Nullable<float> Min (this System.Collections.Generic.IEnumerable<Nullable<float>>
source);
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to determine the minimum value of.
Returns
System.Nullable<System.Single>
A value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the minimum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the
minimum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
int?[] grades = { 78, 92, null, 99, 37, 81 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Nullable<Single>>) method uses the Single implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Nullable<Double>>)
Returns the minimum value in a sequence of nullable Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to determine the minimum value of.
Returns
System.Nullable<System.Double>
A value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the minimum value in the
sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the
minimum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
int?[] grades = { 78, 92, null, 99, 37, 81 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Nullable<Double>>) method uses the Double implementation of IComparable<T> to compare
values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Double>)
Returns the minimum value in a sequence of Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to determine the minimum value of.
Returns
Double
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in
a sequence.
double[] doubles = { 1.5E+104, 9E+103, -2E+103 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Double>) method uses the Double implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Int64>)
Returns the minimum value in a sequence of Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to determine the minimum value of.
Returns
Int64
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in
a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
double[] doubles = { 1.5E+104, 9E+103, -2E+103 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Int64>) method uses the Int64 implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Int32>)
Returns the minimum value in a sequence of Int32 values.
public static int Min (this System.Collections.Generic.IEnumerable<int> source);
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to determine the minimum value of.
Returns
Int32
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in
a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
double[] doubles = { 1.5E+104, 9E+103, -2E+103 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Int32>) method uses the Int32 implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Nullable<Decimal>>)
Returns the minimum value in a sequence of nullable Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to determine the minimum value of.
Returns
System.Nullable<System.Decimal>
A value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the minimum value in
the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min(IEnumerable<Nullable<Int32>>) to determine the
minimum value in a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
int?[] grades = { 78, 92, null, 99, 37, 81 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Nullable<Decimal>>) method uses the Decimal implementation of IComparable<T> to
compare values.
If the source sequence is empty or contains only values that are null , this function returns null .
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min(IEnumerable<Decimal>)
Returns the minimum value in a sequence of Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to determine the minimum value of.
Returns
Decimal
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min(IEnumerable<Double>) to determine the minimum value in
a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
double[] doubles = { 1.5E+104, 9E+103, -2E+103 };
/*
This code produces the following output:
Remarks
The Min(IEnumerable<Decimal>) method uses the Decimal implementation of IComparable<T> to compare values.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,TResult>)
Invokes a transform function on each element of a generic sequence and returns the minimum resulting value.
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by selector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
If type TResult implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if
type TResult implements IComparable, that implementation is used to compare values.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Invokes a transform function on each element of a sequence and returns the minimum Single value.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Single>) method uses the Single implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Single.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Single>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Single value.
public static Nullable<float> Min<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<float>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The value of type Nullable in C# or Nullable(Of Single) in Visual Basic that corresponds to the minimum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Min<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int64>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Int64 value.
public static Nullable<long> Min<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<long>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int64>
The value of type Nullable in C# or Nullable(Of Int64) in Visual Basic that corresponds to the minimum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>) method uses the Int64 implementation
of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Int64> in C# or Nullable(Of Int64) in Visual Basic.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int32>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Int32 value.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int32>
The value of type Nullable in C# or Nullable(Of Int32) in Visual Basic that corresponds to the minimum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>) method uses the Int32 implementation
of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Int32> in C# or Nullable(Of Int32) in Visual Basic.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Double>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Double value.
public static Nullable<double> Min<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<double>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The value of type Nullable in C# or Nullable(Of Double) in Visual Basic that corresponds to the minimum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>) method uses the Double
implementation of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Double> in C# or Nullable(Of Double) in Visual
Basic.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Invokes a transform function on each element of a sequence and returns the minimum Int64 value.
public static long Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,long> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Int64
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Int64>) method uses the Int64 implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Int64.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Invokes a transform function on each element of a sequence and returns the minimum Int32 value.
public static int Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Int32
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>) method uses the Int32 implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Int32.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Invokes a transform function on each element of a sequence and returns the minimum Double value.
public static double Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,double> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Double>) method uses the Double implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Double.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Invokes a transform function on each element of a sequence and returns the minimum Decimal value.
public static decimal Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) method uses the Decimal implementation of
IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Decimal.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>)
Returns the minimum value in a generic sequence.
public static TSource Min<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
Returns
TSource
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>) to determine the
minimum value in a sequence of IComparable<T> objects.
/// <summary>
/// This class implements IComparable in order to
/// be able to compare different Pet objects.
/// </summary>
class Pet : IComparable<Pet>
{
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// Compares this Pet's age to another Pet's age.
/// </summary>
/// <param name="other">The Pet to compare this Pet to.</param>
/// <returns>-1 if this Pet's age is smaller,
/// 0 if the Pets' ages are equal, or
/// 1 if this Pet's age is greater.</returns>
int IComparable<Pet>.CompareTo(Pet other)
{
if (other.Age > this.Age)
return -1;
else if (other.Age == this.Age)
return 0;
else
return 1;
}
}
Console.WriteLine(
"The 'minimum' animal is {0}.",
min.Name);
}
/*
This code produces the following output:
Remarks
If type TSource implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if
type TSource implements IComparable, that implementation is used to compare values.
If TSource is a reference type and the source sequence is empty or contains only values that are null , this function
returns null .
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Min<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Decimal>>)
Invokes a transform function on each element of a sequence and returns the minimum nullable Decimal value.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to determine the minimum value of.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The value of type Nullable in C# or Nullable(Of Decimal) in Visual Basic that corresponds to the minimum value in
the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
to determine the minimum value in a sequence of projected values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The Min<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>) method uses the Decimal
implementation of IComparable<T> to compare values.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Decimal> in C# or Nullable(Of Decimal) in Visual
Basic.
In Visual Basic query expression syntax, an Aggregate Into Min() clause translates to an invocation of Min.
Enumerable.OfType
I n this Article
Filters the elements of an IEnumerable based on a specified type.
public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult> (this
System.Collections.IEnumerable source);
Type Parameters
TResult
The type to filter the elements of the sequence on.
Parameters
source IEnumerable
The IEnumerable whose elements to filter.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains elements from the input sequence of type TResult .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use OfType to filter the elements of an IEnumerable.
System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");
fruits.Add(3.0);
fruits.Add("Banana");
// The following query shows that the standard query operators such as
// Where() can be applied to the ArrayList type after calling OfType().
IEnumerable<string> query2 =
fruits.OfType<string>().Where(fruit => fruit.ToLower().Contains("n"));
Console.WriteLine("
The following strings contain 'n':");
foreach (string fruit in query2)
{
Console.WriteLine(fruit);
}
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The OfType<TResult>(IEnumerable) method returns only those elements in source that can be cast to type TResult .
To instead receive an exception if an element cannot be cast to type TResult , use Cast<TResult>(IEnumerable).
This method is one of the few standard query operator methods that can be applied to a collection that has a non-
parameterized type, such as an ArrayList. This is because OfType extends the type IEnumerable. OfType cannot only be
applied to collections that are based on the parameterized IEnumerable<T> type, but collections that are based on the
non-parameterized IEnumerable type also.
By applying OfType to a collection that implements IEnumerable, you gain the ability to query the collection by using
the standard query operators. For example, specifying a type argument of Object to OfType would return an object of
type IEnumerable<Object> in C# or IEnumerable(Of Object) in Visual Basic, to which the standard query operators
can be applied.
Enumerable.OrderBy
I n this Article
Overloads
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>) Sorts the elements of a sequence in ascending order according
to a key.
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>, IComparer<TKey>) Sorts the elements of a sequence in ascending order by using
a specified comparer.
OrderBy<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>)
Sorts the elements of a sequence in ascending order according to a key.
public static System.Linq.IOrderedEnumerable<TSource> OrderBy<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use OrderBy<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>) to sort the elements of a sequence.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Whiskers - 1
Boots - 4
Barley - 8
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
Two methods are defined to extend the type IOrderedEnumerable<TElement>, which is the return type of this method.
These two methods, namely ThenBy and ThenByDescending , enable you to specify additional sort criteria to sort a
sequence. ThenBy and ThenByDescending also return an IOrderedEnumerable<TElement>, which means any number
of consecutive calls to ThenBy or ThenByDescending can be made.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
This method compares keys by using the default comparer Default.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
In query expression syntax, an orderby (Visual C#) or Order By (Visual Basic) clause translates to an invocation of
OrderBy.
OrderBy<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IComparer<TKey>)
Sorts the elements of a sequence in ascending order by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
Two methods are defined to extend the type IOrderedEnumerable<TElement>, which is the return type of this method.
These two methods, namely ThenBy and ThenByDescending , enable you to specify additional sort criteria to sort a
sequence. ThenBy and ThenByDescending also return an IOrderedEnumerable<TElement>, which means any number
of consecutive calls to ThenBy or ThenByDescending can be made.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
If comparer is null , the default comparer Default is used to compare keys.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
Enumerable.OrderByDescending
I n this Article
Overloads
OrderByDescending<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IComparer<TKey>) Sorts the elements of a sequence in descending order by using
a specified comparer.
OrderByDescending<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>) Sorts the elements of a sequence in descending order
according to a key.
OrderByDescending<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IComparer<TKey>)
Sorts the elements of a sequence in descending order by using a specified comparer.
public static System.Linq.IOrderedEnumerable<TSource> OrderByDescending<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use OrderByDescending<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IComparer<TKey>) to sort the elements of a sequence in descending order by using a transform
function and a custom comparer.
/// <summary>
/// This IComparer class sorts by the fractional part of the decimal number.
/// </summary>
public class SpecialComparer : IComparer<decimal>
{
/// <summary>
/// Compare two decimal numbers by their fractional parts.
/// </summary>
/// <param name="d1">The first decimal to compare.</param>
/// <param name="d2">The second decimal to compare.</param>
/// <returns>1 if the first decimal's fractional part
/// is greater than the second decimal's fractional part,
/// -1 if the first decimal's fractional
/// part is less than the second decimal's fractional part,
/// or the result of calling Decimal.Compare()
/// if the fractional parts are equal.</returns>
public int Compare(decimal d1, decimal d2)
{
decimal fractional1, fractional2;
if (fractional1 == fractional2)
return Decimal.Compare(d1, d2);
else if (fractional1 > fractional2)
return 1;
else
return -1;
}
}
IEnumerable<decimal> query =
decimals.OrderByDescending(num =>
num, new SpecialComparer());
/*
This code produces the following output:
9.7
0.5
8.3
6.3
1.3
6.2
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
Two methods are defined to extend the type IOrderedEnumerable<TElement>, which is the return type of this method.
These two methods, namely ThenBy and ThenByDescending , enable you to specify additional sort criteria to sort a
sequence. ThenBy and ThenByDescending also return an IOrderedEnumerable<TElement>, which means any number
of consecutive calls to ThenBy or ThenByDescending can be made.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
If comparer is null , the default comparer Default is used to compare keys.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
OrderByDescending<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>)
Sorts the elements of a sequence in descending order according to a key.
public static System.Linq.IOrderedEnumerable<TSource> OrderByDescending<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
keySelector System.Func<TSource,TKey>
A function to extract a key from an element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
This method compares keys by using the default comparer Default.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
In query expression syntax, an orderby descending (Visual C#) or Order By Descending (Visual Basic) clause
translates to an invocation of OrderByDescending.
Enumerable.Prepend
I n this Article
public static System.Collections.Generic.IEnumerable<TSource> Prepend<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, TSource element);
Type Parameters
TSource
Parameters
source System.Collections.Generic.IEnumerable<TSource>
element TSource
Returns
System.Collections.Generic.IEnumerable<TSource>
Enumerable.Range
I n this Article
Generates a sequence of integral numbers within a specified range.
public static System.Collections.Generic.IEnumerable<int> Range (int start, int count);
Parameters
start Int32
The value of the first integer in the sequence.
count Int32
The number of sequential integers to generate.
Returns
System.Collections.Generic.IEnumerable<System.Int32>
An IEnumerable in C# or IEnumerable(Of Int32) in Visual Basic that contains a range of sequential integral numbers.
Exceptions
ArgumentOutOfRangeException
count is less than 0.
-or-
start + count -1 is larger than MaxValue.
Examples
The following code example demonstrates how to use Range to generate a sequence of values.
// Generate a sequence of integers from 1 to 10
// and then select their squares.
IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);
/*
This code produces the following output:
1
4
9
16
25
36
49
64
81
100
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Enumerable.Repeat
I n this Article
Generates a sequence that contains one repeated value.
public static System.Collections.Generic.IEnumerable<TResult> Repeat<TResult> (TResult element, int
count);
Type Parameters
TResult
The type of the value to be repeated in the result sequence.
Parameters
element TResult
The value to be repeated.
count Int32
The number of times to repeat the value in the generated sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains a repeated value.
Exceptions
ArgumentOutOfRangeException
count is less than 0.
Examples
The following code example demonstrates how to use Repeat to generate a sequence of a repeated value.
IEnumerable<string> strings =
Enumerable.Repeat("I like programming.", 15);
/*
This code produces the following output:
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
I like programming.
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Enumerable.Reverse
I n this Article
Inverts the order of the elements in a sequence.
public static System.Collections.Generic.IEnumerable<TSource> Reverse<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to reverse.
Returns
System.Collections.Generic.IEnumerable<TSource>
A sequence whose elements correspond to those of the input sequence in reverse order.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Reverse to reverse the order of elements in an array.
char[] apple = { 'a', 'p', 'p', 'l', 'e' };
/*
This code produces the following output:
e l p p a
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Unlike OrderBy, this sorting method does not consider the actual values themselves in determining the order. Rather, it
just returns the elements in the reverse order from which they are produced by the underlying source.
Enumerable.Select
I n this Article
Overloads
Select<TSource,TResult>(IEnumerable<TSource>, Func<
TSource,Int32,TResult>) Projects each element of a sequence into a new form by
incorporating the element's index.
Select<TSource,TResult>(IEnumerable<TSource>, Func<
TSource,TResult>) Projects each element of a sequence into a new form.
Select<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,TResult>)
Projects each element of a sequence into a new form by incorporating the element's index.
public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,TResult> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by selector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to invoke a transform function on.
selector System.Func<TSource,System.Int32,TResult>
A transform function to apply to each source element; the second parameter of the function represents the index of the
source element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source .
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Select<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,TResult>) to project over a sequence of values and use the index of each element.
string[] fruits = { "apple", "banana", "mango", "orange",
"passionfruit", "grape" };
var query =
fruits.Select((fruit, index) =>
new { index, str = fruit.Substring(0, index) });
/*
This code produces the following output:
{index=0, str=}
{index=1, str=b}
{index=2, str=ma}
{index=3, str=ora}
{index=4, str=pass}
{index=5, str=grape}
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The first argument to selector represents the element to process. The second argument to selector represents the
zero-based index of that element in the source sequence. This can be useful if the elements are in a known order and
you want to do something with an element at a particular index, for example. It can also be useful if you want to
retrieve the index of one or more elements.
This projection method requires the transform function, selector , to produce one value for each value in the source
sequence, source . If selector returns a value that is itself a collection, it is up to the consumer to traverse the
subsequences manually. In such a situation, it might be better for your query to return a single coalesced sequence of
values. To achieve this, use the SelectMany method instead of Select. Although SelectMany works similarly to Select ,
it differs in that the transform function returns a collection that is then expanded by SelectMany before it is returned.
Select<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,TResult>)
Projects each element of a sequence into a new form.
public static System.Collections.Generic.IEnumerable<TResult> Select<TSource,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TResult> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by selector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to invoke a transform function on.
selector System.Func<TSource,TResult>
A transform function to apply to each element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source .
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Select<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,TResult>) to project over a sequence of values.
IEnumerable<int> squares =
Enumerable.Range(1, 10).Select(x => x * x);
1
4
9
16
25
36
49
64
81
100
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
This projection method requires the transform function, selector , to produce one value for each value in the source
sequence, source . If selector returns a value that is itself a collection, it is up to the consumer to traverse the
subsequences manually. In such a situation, it might be better for your query to return a single coalesced sequence of
values. To achieve this, use the SelectMany method instead of Select. Although SelectMany works similarly to Select ,
it differs in that the transform function returns a collection that is then expanded by SelectMany before it is returned.
In query expression syntax, a select (Visual C#) or Select (Visual Basic) clause translates to an invocation of Select.
Enumerable.SelectMany
I n this Article
Overloads
SelectMany<TSource,TCollection,TResult>(IEnumerable<
TSource>, Func<TSource,IEnumerable<TCollection>>, Func< Projects each element of a sequence to an IEnumerable<T>,
TSource,TCollection,TResult>) flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein.
SelectMany<TSource,TCollection,TResult>(IEnumerable<
TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Projects each element of a sequence to an IEnumerable<T>,
Func<TSource,TCollection,TResult>) flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein. The
index of each source element is used in the intermediate
projected form of that element.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<
TSource,IEnumerable<TResult>>) Projects each element of a sequence to an IEnumerable<T>
and flattens the resulting sequences into one sequence.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<
TSource,Int32,IEnumerable<TResult>>) Projects each element of a sequence to an IEnumerable<T>,
and flattens the resulting sequences into one sequence. The
index of each source element is used in the projected form of
that element.
SelectMany<TSource,TCollection,TResult>
(IEnumerable<TSource>,
Func<TSource,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein.
public static System.Collections.Generic.IEnumerable<TResult>
SelectMany<TSource,TCollection,TResult> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector,
Func<TSource,TCollection,TResult> resultSelector);
Type Parameters
TSource
The type of the elements of source .
TCollection
The type of the intermediate elements collected by collectionSelector .
TResult
The type of the elements of the resulting sequence.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
collectionSelector System.Func<TSource,System.Collections.Generic.IEnumerable<TCollection>>
A transform function to apply to each element of the input sequence.
resultSelector System.Func<TSource,TCollection,TResult>
A transform function to apply to each element of the intermediate sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function
collectionSelector on each element of source and then mapping each of those sequence elements and their
corresponding source element to a result element.
Exceptions
ArgumentNullException
source or collectionSelector or resultSelector is null .
Examples
The following code example demonstrates how to use SelectMany<TSource,TCollection,TResult>
(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) to perform
a one-to-many projection over an array and use a result selector function to keep each corresponding element from
the source sequence in scope for the final call to Select .
class PetOwner
{
public string Name { get; set; }
public List<string> Pets { get; set; }
}
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
In query expression syntax, each from clause (Visual C#) or From clause (Visual Basic) after the initial one translates to
an invocation of SelectMany.
SelectMany<TSource,TCollection,TResult>
(IEnumerable<TSource>,
Func<TSource,Int32,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein. The index of each source element is used in the intermediate
projected form of that element.
Type Parameters
TSource
The type of the elements of source .
TCollection
The type of the intermediate elements collected by collectionSelector .
TResult
The type of the elements of the resulting sequence.
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
collectionSelector System.Func<TSource,System.Int32,System.Collections.Generic.IEnumerable<TCollection>>
A transform function to apply to each source element; the second parameter of the function represents the index of the
source element.
resultSelector System.Func<TSource,TCollection,TResult>
A transform function to apply to each element of the intermediate sequence.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function
collectionSelector on each element of source and then mapping each of those sequence elements and their
corresponding source element to a result element.
Exceptions
ArgumentNullException
source or collectionSelector or resultSelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) method is useful when you have
to keep the elements of source in scope for query logic that occurs after the call to
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>). See the Example section for a code example. If there is a bidirectional relationship
between objects of type TSource and objects of type TCollection , that is, if an object of type TCollection provides a
property to retrieve the TSource object that produced it, you do not need this overload of
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>). Instead, you can use SelectMany<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,IEnumerable<TResult>>) and navigate back to the TSource object through the TCollection
object.
SelectMany<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,IEnumerable<TResult>>)
Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the elements of the sequence returned by selector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
selector System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>
A transform function to apply to each element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of
the input sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use SelectMany<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,IEnumerable<TResult>>) to perform a one-to-many projection over an array.
class PetOwner
{
public string Name { get; set; }
public List<String> Pets { get; set; }
}
Console.WriteLine("Using SelectMany():");
Console.WriteLine("
Using Select():");
/*
This code produces the following output:
Using SelectMany():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
Using Select():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
In query expression syntax, each from clause (Visual C#) or From clause (Visual Basic) after the initial one translates to
an invocation of SelectMany.
SelectMany<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,IEnumerable<TResult>>)
Projects each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into one sequence.
The index of each source element is used in the projected form of that element.
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult> (this
System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the elements of the sequence returned by selector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
selector System.Func<TSource,System.Int32,System.Collections.Generic.IEnumerable<TResult>>
A transform function to apply to each source element; the second parameter of the function represents the index of the
source element.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of
an input sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use SelectMany<TSource,TResult>(IEnumerable<TSource>,
Func<TSource,Int32,IEnumerable<TResult>>) to perform a one-to-many projection over an array and use the index of
each outer element.
class PetOwner
{
public string Name { get; set; }
public List<string> Pets { get; set; }
}
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The first argument to selector represents the element to process. The second argument to selector represents the
zero-based index of that element in the source sequence. This can be useful if the elements are in a known order and
you want to do something with an element at a particular index, for example. It can also be useful if you want to
retrieve the index of one or more elements.
Enumerable.SequenceEqual
I n this Article
Overloads
SequenceEqual<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>) Determines whether two sequences are equal by comparing
the elements by using the default equality comparer for their
type.
SequenceEqual<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) Determines whether two sequences are equal by comparing
their elements by using a specified IEqualityComparer<T>.
SequenceEqual<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>)
Determines whether two sequences are equal by comparing the elements by using the default equality comparer for
their type.
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource>
first, System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to second .
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to the first sequence.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the
default equality comparer for their type; otherwise, false .
Exceptions
ArgumentNullException
first or second is null .
Examples
The following code examples demonstrate how to use SequenceEqual<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>) to determine whether two sequences are equal. In the first two examples, the method
determines whether the compared sequences contain references to the same objects. In the third and fourth examples,
the method compares the actual data of the objects within the sequences.
In this example the sequences are equal.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The lists {0} equal.",
equal ? "are" : "are not");
}
/*
This code produces the following output:
The following code example compares two sequences that are not equal. Note that the sequences contain identical
data, but because the objects that they contain have different references, the sequences are not considered equal.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
After you implement this interface, you can use sequences of ProductA objects in the SequenceEqual<TSource>
(IEnumerable<TSource>, IEnumerable<TSource>) method, as shown in the following example.
/*
This code produces the following output:
Equal? True
*/
Remarks
The SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method enumerates the two source
sequences in parallel and compares corresponding elements by using the default equality comparer for TSource ,
Default. The default equality comparer, Default, is used to compare values of the types that implement the
IEqualityComparer<T> generic interface. To compare a custom data type, you need to implement this interface and
provide your own GetHashCode and Equals methods for the type.
SequenceEqual<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
Determines whether two sequences are equal by comparing their elements by using a specified IEqualityComparer<T>.
public static bool SequenceEqual<TSource> (this System.Collections.Generic.IEnumerable<TSource>
first, System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to second .
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to compare to the first sequence.
comparer System.Collections.Generic.IEqualityComparer<TSource>
An IEqualityComparer<T> to use to compare elements.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements compare equal according to
comparer ; otherwise, false .
Exceptions
ArgumentNullException
first or second is null .
Examples
The following example shows how to implement an equality comparer that can be used in the
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method.
public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this comparer, you can use sequences of Product objects in the SequenceEqual<TSource>
(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method, as shown in the following
example.
Product[] storeA = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 } };
/*
This code produces the following output:
Equal? True
*/
Remarks
The SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
method enumerates the two source sequences in parallel and compares corresponding elements by using the specified
IEqualityComparer<T>. If c omparer is null , the default equality comparer, Default, is used to compare elements.
Enumerable.Single
I n this Article
Overloads
Single<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Returns the only element of a sequence that satisfies a
specified condition, and throws an exception if more than one
such element exists.
Single<TSource>(IEnumerable<TSource>)
Returns the only element of a sequence, and throws an
exception if there is not exactly one element in the sequence.
Single<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one
such element exists.
public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return a single element from.
predicate System.Func<TSource,System.Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies a condition.
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
No element satisfies the condition in predicate .
-or-
More than one element satisfies the condition in predicate .
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use Single<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to select the only element of an array that satisfies a condition.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
Console.WriteLine(fruit1);
/*
This code produces the following output:
passionfruit
*/
try
{
fruit2 = fruits.Single(fruit => fruit.Length > 15);
}
catch (System.InvalidOperationException)
{
Console.WriteLine(@"The collection does not contain exactly
one element whose length is greater than 15.");
}
Console.WriteLine(fruit2);
Remarks
The Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method throws an exception if the input
sequence contains no matching element. To instead return null when no matching element is found, use
SingleOrDefault.
Single<TSource>(IEnumerable<TSource>)
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
public static TSource Single<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the single element of.
Returns
TSource
The single element of the input sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
The input sequence contains more than one element.
-or-
The input sequence is empty.
Examples
The following code example demonstrates how to use Single<TSource>(IEnumerable<TSource>) to select the only
element of an array.
string[] fruits1 = { "orange" };
Console.WriteLine(fruit1);
/*
This code produces the following output:
orange
*/
The following code example demonstrates that Single<TSource>(IEnumerable<TSource>) throws an exception when
the sequence does not contain exactly one element.
string[] fruits2 = { "orange", "apple" };
string fruit2 = null;
try
{
fruit2 = fruits2.Single();
}
catch (System.InvalidOperationException)
{
Console.WriteLine("The collection does not contain exactly one element.");
}
Console.WriteLine(fruit2);
/*
This code produces the following output:
Remarks
The Single<TSource>(IEnumerable<TSource>) method throws an exception if the input sequence is empty. To instead
return null when the input sequence is empty, use SingleOrDefault.
Enumerable.SingleOrDefault
I n this Article
Overloads
SingleOrDefault<TSource>(IEnumerable<TSource>)
Returns the only element of a sequence, or a default value if
the sequence is empty; this method throws an exception if
there is more than one element in the sequence.
SingleOrDefault<TSource>(IEnumerable<TSource>, Func<
TSource,Boolean>) Returns the only element of a sequence that satisfies a
specified condition or a default value if no such element exists;
this method throws an exception if more than one element
satisfies the condition.
SingleOrDefault<TSource>(IEnumerable<TSource>)
Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if
there is more than one element in the sequence.
public static TSource SingleOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return the single element of.
Returns
TSource
The single element of the input sequence, or default ( TSource ) if the sequence contains no elements.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
The input sequence contains more than one element.
Examples
The following code example demonstrates how to use SingleOrDefault<TSource>(IEnumerable<TSource>) to select
the only element of an array.
string[] fruits1 = { "orange" };
Console.WriteLine(fruit1);
/*
This code produces the following output:
orange
*/
Console.WriteLine(
String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);
/*
This code produces the following output:
No such string!
*/
Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no
elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use
the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method to specify the default value that you want to
use if the collection is empty. Then, call Single<TSource>(IEnumerable<TSource>) to obtain the element. The following
code example uses both techniques to obtain a default value of 1 if a collection of page numbers is empty. Because the
default value for an integer is 0, which is not usually a valid page number, the default value must be specified as 1
instead. The first result variable is checked for the unwanted default value after the query has finished executing. The
second result variable is obtained by using DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) to specify a
default value of 1.
int[] pageNumbers = { };
/*
This code produces the following output:
The SingleOrDefault method does not provide a way to specify a default value. If you want to specify a default value
other than default(TSource) , use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as
described in the Example section.
SingleOrDefault<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists;
this method throws an exception if more than one element satisfies the condition.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return a single element from.
predicate System.Func<TSource,System.Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition, or default ( TSource ) if no such element is found.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use SingleOrDefault<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to select the only element of an array that satisfies a condition.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
Console.WriteLine(fruit1);
/*
This code produces the following output:
passionfruit
*/
string fruit2 =
fruits.SingleOrDefault(fruit => fruit.Length > 15);
Console.WriteLine(
String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);
/*
This code produces the following output:
No such string!
*/
Remarks
The default value for reference and nullable types is null .
Enumerable.Skip
I n this Article
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
public static System.Collections.Generic.IEnumerable<TSource> Skip<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return elements from.
count Int32
The number of elements to skip before returning the remaining elements.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements that occur after the specified index in the input sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Skip to skip a specified number of elements in a sorted array and
return the remaining elements.
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
IEnumerable<int> lowerGrades =
grades.OrderByDescending(g => g).Skip(3);
/*
This code produces the following output:
If source contains fewer than count elements, an empty IEnumerable<T> is returned. If count is less than or equal to
zero, all elements of source are yielded.
The Take and Skip methods are functional complements. Given a sequence coll and an integer n , concatenating the
results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll .
In Visual Basic query expression syntax, a Skip clause translates to an invocation of Skip.
Enumerable.SkipLast
I n this Article
public static System.Collections.Generic.IEnumerable<TSource> SkipLast<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Type Parameters
TSource
Parameters
source System.Collections.Generic.IEnumerable<TSource>
count Int32
Returns
System.Collections.Generic.IEnumerable<TSource>
Enumerable.SkipWhile
I n this Article
Overloads
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Bypasses elements in a sequence as long as a specified
condition is true and then returns the remaining elements.
SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,
Int32,Boolean>) Bypasses elements in a sequence as long as a specified
condition is true and then returns the remaining elements. The
element's index is used in the logic of the predicate function.
SkipWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return elements from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from the input sequence starting at the first element in the linear series
that does not pass the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use SkipWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to skip elements of an array as long as a condition is true.
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
IEnumerable<int> lowerGrades =
grades
.OrderByDescending(grade => grade)
.SkipWhile(grade => grade >= 80);
/*
This code produces the following output:
Remarks
The SkipWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method is implemented by using deferred
execution. The immediate return value is an object that stores all the information that is required to perform the action.
The query represented by this method is not executed until the object is enumerated either by calling its
GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
This method tests each element of source by using predicate and skips the element if the result is true . After the
predicate function returns false for an element, that element and the remaining elements in source are yielded and
there are no more invocations of predicate .
If predicate returns true for all elements in the sequence, an empty IEnumerable<T> is returned.
The TakeWhile and SkipWhile methods are functional complements. Given a sequence coll and a pure function p ,
concatenating the results of coll.TakeWhile(p) and coll.SkipWhile(p) yields the same sequence as coll .
In Visual Basic query expression syntax, a Skip While clause translates to an invocation of SkipWhile.
SkipWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Int32,Boolean>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The
element's index is used in the logic of the predicate function.
public static System.Collections.Generic.IEnumerable<TSource> SkipWhile<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to return elements from.
predicate System.Func<TSource,System.Int32,System.Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the
source element.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from the input sequence starting at the first element in the linear series
that does not pass the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use SkipWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Int32,Boolean>) to skip elements of an array as long as a condition that depends on the element's index
is true.
IEnumerable<int> query =
amounts.SkipWhile((amount, index) => amount > index * 1000);
/*
This code produces the following output:
4000
1500
5500
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
If predicate returns true for all elements in the sequence, an empty IEnumerable<T> is returned.
The first argument of predicate represents the element to test. The second argument represents the zero-based index
of the element within source .
The TakeWhile and SkipWhile methods are functional complements. Given a sequence coll and a pure function p ,
concatenating the results of coll.TakeWhile(p) and coll.SkipWhile(p) yields the same sequence as coll .
In Visual Basic query expression syntax, a Skip While clause translates to an invocation of SkipWhile.
Enumerable.Sum
I n this Article
Overloads
Sum(IEnumerable<Nullable<Int32>>)
Computes the sum of a sequence of nullable Int32 values.
Sum(IEnumerable<Nullable<Single>>)
Computes the sum of a sequence of nullable Single values.
Sum(IEnumerable<Single>)
Computes the sum of a sequence of Single values.
Sum(IEnumerable<Nullable<Int64>>)
Computes the sum of a sequence of nullable Int64 values.
Sum(IEnumerable<Nullable<Double>>)
Computes the sum of a sequence of nullable Double values.
Sum(IEnumerable<Double>)
Computes the sum of a sequence of Double values.
Sum(IEnumerable<Int64>)
Computes the sum of a sequence of Int64 values.
Sum(IEnumerable<Int32>)
Computes the sum of a sequence of Int32 values.
Sum(IEnumerable<Decimal>)
Computes the sum of a sequence of Decimal values.
Sum(IEnumerable<Nullable<Decimal>>)
Computes the sum of a sequence of nullable Decimal values.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Single>) Computes the sum of the sequence of Single values that are
obtained by invoking a transform function on each element of
the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Single>>) Computes the sum of the sequence of nullable Single values
that are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int64>>) Computes the sum of the sequence of nullable Int64 values
that are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Int32>>) Computes the sum of the sequence of nullable Int32 values
that are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Double>>) Computes the sum of the sequence of nullable Double values
that are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Nullable<Decimal>>) Computes the sum of the sequence of nullable Decimal values
that are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Int64>) Computes the sum of the sequence of Int64 values that are
obtained by invoking a transform function on each element of
the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Int32>) Computes the sum of the sequence of Int32 values that are
obtained by invoking a transform function on each element of
the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Double>) Computes the sum of the sequence of Double values that are
obtained by invoking a transform function on each element of
the input sequence.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,
Decimal>) Computes the sum of the sequence of Decimal values that are
obtained by invoking a transform function on each element of
the input sequence.
Sum(IEnumerable<Nullable<Int32>>)
Computes the sum of a sequence of nullable Int32 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int32>>
A sequence of nullable Int32 values to calculate the sum of.
Returns
System.Nullable<System.Int32>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IEnumerable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
The result does not include values that are null .
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Nullable<Single>>)
Computes the sum of a sequence of nullable Single values.
public static Nullable<float> Sum (this System.Collections.Generic.IEnumerable<Nullable<float>>
source);
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Single>>
A sequence of nullable Single values to calculate the sum of.
Returns
System.Nullable<System.Single>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IEnumerable<Nullable<Single>>) to sum the values of a
sequence.
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Single>)
Computes the sum of a sequence of Single values.
public static float Sum (this System.Collections.Generic.IEnumerable<float> source);
Parameters
source System.Collections.Generic.IEnumerable<System.Single>
A sequence of Single values to calculate the sum of.
Returns
Single
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IEnumerable<Single>) to sum the values of a sequence.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Nullable<Int64>>)
Computes the sum of a sequence of nullable Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Int64>>
A sequence of nullable Int64 values to calculate the sum of.
Returns
System.Nullable<System.Int64>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IEnumerable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Nullable<Double>>)
Computes the sum of a sequence of nullable Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Double>>
A sequence of nullable Double values to calculate the sum of.
Returns
System.Nullable<System.Double>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IEnumerable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
The result does not include values that are null .
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Double>)
Computes the sum of a sequence of Double values.
Parameters
source System.Collections.Generic.IEnumerable<System.Double>
A sequence of Double values to calculate the sum of.
Returns
Double
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IEnumerable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Int64>)
Computes the sum of a sequence of Int64 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Int64>
A sequence of Int64 values to calculate the sum of.
Returns
Int64
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IEnumerable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Int32>)
Computes the sum of a sequence of Int32 values.
Parameters
source System.Collections.Generic.IEnumerable<System.Int32>
A sequence of Int32 values to calculate the sum of.
Returns
Int32
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IEnumerable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Decimal>)
Computes the sum of a sequence of Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Decimal>
A sequence of Decimal values to calculate the sum of.
Returns
Decimal
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IEnumerable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
The Sum(IEnumerable<Decimal>) method returns zero if source contains no elements.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum(IEnumerable<Nullable<Decimal>>)
Computes the sum of a sequence of nullable Decimal values.
Parameters
source System.Collections.Generic.IEnumerable<System.Nullable<System.Decimal>>
A sequence of nullable Decimal values to calculate the sum of.
Returns
System.Nullable<System.Decimal>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IEnumerable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)
Computes the sum of the sequence of Single values that are obtained by invoking a transform function on each
element of the input sequence.
public static float Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,float> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Single>
A transform function to apply to each element.
Returns
Single
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
The Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>) method returns zero if source contains no
elements.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Single.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Single>>)
Computes the sum of the sequence of nullable Single values that are obtained by invoking a transform function on
each element of the input sequence.
public static Nullable<float> Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<float>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Single>>
A transform function to apply to each element.
Returns
System.Nullable<System.Single>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int64>>)
Computes the sum of the sequence of nullable Int64 values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Int64>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int64>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
The result does not include values that are null .
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Int64> in C# or Nullable(Of Int64) in Visual Basic
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Int32>>)
Computes the sum of the sequence of nullable Int32 values that are obtained by invoking a transform function on each
element of the input sequence.
public static Nullable<int> Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<int>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Int32>>
A transform function to apply to each element.
Returns
System.Nullable<System.Int32>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
The result does not include values that are null .
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Nullable<Int32> in C# or Nullable(Of Int32) in Visual Basic.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Double>>)
Computes the sum of the sequence of nullable Double values that are obtained by invoking a transform function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Double>>
A transform function to apply to each element.
Returns
System.Nullable<System.Double>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Nullable<Decimal>>)
Computes the sum of the sequence of nullable Decimal values that are obtained by invoking a transform function on
each element of the input sequence.
public static Nullable<decimal> Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source, Func<TSource,Nullable<decimal>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Nullable<System.Decimal>>
A transform function to apply to each element.
Returns
System.Nullable<System.Decimal>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)
Computes the sum of the sequence of Int64 values that are obtained by invoking a transform function on each element
of the input sequence.
public static long Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,long> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Int64>
A transform function to apply to each element.
Returns
Int64
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Int64.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)
Computes the sum of the sequence of Int32 values that are obtained by invoking a transform function on each element
of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Int32>
A transform function to apply to each element.
Returns
Int32
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Int32.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>)
Computes the sum of the sequence of Double values that are obtained by invoking a transform function on each
element of the input sequence.
public static double Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,double> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Double>
A transform function to apply to each element.
Returns
Double
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Double.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Sum<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)
Computes the sum of the sequence of Decimal values that are obtained by invoking a transform function on each
element of the input sequence.
public static decimal Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,decimal> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence of values that are used to calculate a sum.
selector System.Func<TSource,System.Decimal>
A transform function to apply to each element.
Returns
Decimal
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IEnumerable<TSource>,
Func<TSource,Double>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method returns zero if source contains no elements.
You can apply this method to a sequence of arbitrary values if you provide a function, selector , that projects the
members of source into a numeric type, specifically Decimal.
In Visual Basic query expression syntax, an Aggregate Into Sum() clause translates to an invocation of Sum.
Enumerable.Take
I n this Article
Returns a specified number of contiguous elements from the start of a sequence.
public static System.Collections.Generic.IEnumerable<TSource> Take<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return elements from.
count Int32
The number of elements to return.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the specified number of elements from the start of the input sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Take to return elements from the start of a sequence.
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
IEnumerable<int> topThreeGrades =
grades.OrderByDescending(grade => grade).Take(3);
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
Take enumerates source and yields elements until count elements have been yielded or source contains no more
elements. If count exceeds the number of elements in source , all elements of source are returned.
If count is less than or equal to zero, source is not enumerated and an empty IEnumerable<T> is returned.
The Take and Skip methods are functional complements. Given a sequence coll and an integer n , concatenating the
results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll .
In Visual Basic query expression syntax, a Take clause translates to an invocation of Take.
Enumerable.TakeLast
I n this Article
public static System.Collections.Generic.IEnumerable<TSource> TakeLast<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, int count);
Type Parameters
TSource
Parameters
source System.Collections.Generic.IEnumerable<TSource>
count Int32
Returns
System.Collections.Generic.IEnumerable<TSource>
Enumerable.TakeWhile
I n this Article
Overloads
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Returns elements from a sequence as long as a specified
condition is true.
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,
Int32,Boolean>) Returns elements from a sequence as long as a specified
condition is true. The element's index is used in the logic of the
predicate function.
TakeWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Returns elements from a sequence as long as a specified condition is true.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
A sequence to return elements from.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from the input sequence that occur before the element at which the test
no longer passes.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use TakeWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to return elements from the start of a sequence as long as a condition is true.
string[] fruits = { "apple", "banana", "mango", "orange",
"passionfruit", "grape" };
IEnumerable<string> query =
fruits.TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);
/*
This code produces the following output:
apple
banana
mango
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The TakeWhile and SkipWhile methods are functional complements. Given a sequence coll and a pure function p ,
concatenating the results of coll.TakeWhile(p) and coll.SkipWhile(p) yields the same sequence as coll .
In Visual Basic query expression syntax, a Take While clause translates to an invocation of TakeWhile.
TakeWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Int32,Boolean>)
Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of
the predicate function.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The sequence to return elements from.
predicate System.Func<TSource,System.Int32,System.Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the
source element.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains elements from the input sequence that occur before the element at which the test no
longer passes.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use TakeWhile<TSource>(IEnumerable<TSource>,
Func<TSource,Int32,Boolean>) to return elements from the start of a sequence as long as a condition that uses the
element's index is true.
string[] fruits = { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
IEnumerable<string> query =
fruits.TakeWhile((fruit, index) => fruit.Length >= index);
/*
This code produces the following output:
apple
passionfruit
banana
mango
orange
blueberry
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The first argument of predicate represents the element to test. The second argument represents the zero-based index
of the element within source .
The TakeWhile and SkipWhile methods are functional complements. Given a sequence coll and a pure function p ,
concatenating the results of coll.TakeWhile(p) and coll.SkipWhile(p) yields the same sequence as coll .
In Visual Basic query expression syntax, a Take While clause translates to an invocation of TakeWhile.
Enumerable.ThenBy
I n this Article
Overloads
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>,
Func<TSource,TKey>, IComparer<TKey>) Performs a subsequent ordering of the elements in a sequence
in ascending order by using a specified comparer.
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>,
Func<TSource,TKey>) Performs a subsequent ordering of the elements in a sequence
in ascending order according to a key.
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>,
Func<TSource,TKey>, IComparer<TKey>)
Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this
System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the
return type of these methods. This design enables you to specify multiple sort criteria by applying any number of
ThenBy or ThenByDescending methods.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
If comparer is null , the default comparer Default is used to compare keys.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>,
Func<TSource,TKey>)
Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>,
Func<TSource,TKey>) to perform a secondary ordering of the elements in a sequence.
/*
This code produces the following output:
apple
grape
mango
banana
orange
blueberry
raspberry
passionfruit
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the
return type of these methods. This design enables you to specify multiple sort criteria by applying any number of
ThenBy or ThenByDescending methods.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
This method compares keys by using the default comparer Default.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
In query expression syntax, an orderby [first criterion], [second criterion] (Visual C#) or
Order By [first criterion], [second criterion] (Visual Basic) clause translates to an invocation of ThenBy.
Enumerable.ThenByDescending
I n this Article
Overloads
ThenByDescending<TSource,TKey>(IOrderedEnumerable<
TSource>, Func<TSource,TKey>) Performs a subsequent ordering of the elements in a sequence
in descending order, according to a key.
ThenByDescending<TSource,TKey>(IOrderedEnumerable<
TSource>, Func<TSource,TKey>, IComparer<TKey>) Performs a subsequent ordering of the elements in a sequence
in descending order by using a specified comparer.
ThenByDescending<TSource,TKey>
(IOrderedEnumerable<TSource>, Func<TSource,TKey>)
Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
public static System.Linq.IOrderedEnumerable<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the
return type of these methods. This design enables you to specify multiple sort criteria by applying any number of
ThenBy or ThenByDescending methods.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
This sorting method compares keys by using the default comparer Default.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
In Visual C# query expression syntax, an orderby [first criterion], [second criterion] descending clause
translates to an invocation of ThenByDescending.
In Visual Basic query expression syntax, an Order By [first criterion], [second criterion] Descending clause
translates to an invocation of ThenByDescending.
ThenByDescending<TSource,TKey>
(IOrderedEnumerable<TSource>, Func<TSource,TKey>,
IComparer<TKey>)
Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> that contains elements to sort.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.
Returns
System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use ThenByDescending<TSource,TKey>
(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) to perform a secondary ordering of the
elements in a sequence in descending order by using a custom comparer.
public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, true);
}
}
/*
This code produces the following output:
apPLe
apple
APple
apPLE
orange
ORANGE
baNanA
BAnana
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
To order a sequence by the values of the elements themselves, specify the identity function ( x => x in Visual C# or
Function(x) x in Visual Basic) for keySelector .
ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the
return type of these methods. This design enables you to specify multiple sort criteria by applying any number of
ThenBy or ThenByDescending methods.
Note
Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending
on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new
primary ordering that ignores the previously established ordering.
If comparer is null , the default comparer Default is used to compare keys.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.
In contrast, an unstable sort does not preserve the order of elements that have the same key.
Enumerable.ToArray
I n this Article
Creates an array from a IEnumerable<T>.
public static TSource[] ToArray<TSource> (this System.Collections.Generic.IEnumerable<TSource>
source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create an array from.
Returns
TSource[]
An array that contains the elements from the input sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use ToArray to force immediate query evaluation and return an array
of results.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Coho Vineyard
Lucerne Publishing
Wingtip Toys
Adventure Works
*/
Remarks
The ToArray<TSource>(IEnumerable<TSource>) method forces immediate query evaluation and returns an array that
contains the query results. You can append this method to your query in order to obtain a cached copy of the query
results.
ToList has similar behavior but returns a List<T> instead of an array.
Enumerable.ToDictionary
I n this Article
Overloads
ToDictionary<TSource,TKey,TElement>(IEnumerable<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>) Creates a Dictionary<TKey,TValue> from an IEnumerable<T>
according to specified key selector and element selector
functions.
ToDictionary<TSource,TKey,TElement>(IEnumerable<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Creates a Dictionary<TKey,TValue> from an IEnumerable<T>
IEqualityComparer<TKey>) according to a specified key selector function, a comparer, and
an element selector function.
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>) Creates a Dictionary<TKey,TValue> from an IEnumerable<T>
according to a specified key selector function.
ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>, IEqualityComparer<TKey>) Creates a Dictionary<TKey,TValue> from an IEnumerable<T>
according to a specified key selector function and key
comparer.
ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>)
Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to specified key selector and element selector
functions.
public static System.Collections.Generic.Dictionary<TKey,TElement>
ToDictionary<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
System.Collections.Generic.Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
-or-
keySelector produces a key that is null .
ArgumentException
keySelector produces duplicate keys for two elements.
Remarks
The ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
method uses the default equality comparer Default to compare keys.
ToDictionary<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function, a
comparer, and an element selector function.
public static System.Collections.Generic.Dictionary<TKey,TElement>
ToDictionary<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
-or-
keySelector produces a key that is null .
ArgumentException
keySelector produces duplicate keys for two elements.
Remarks
If comparer is null , the default equality comparer Default is used to compare keys.
ToDictionary<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>)
Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function.
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Collections.Generic.Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
Exceptions
ArgumentNullException
source or keySelector is null .
-or-
keySelector produces a key that is null .
ArgumentException
keySelector produces duplicate keys for two elements.
Examples
The following code example demonstrates how to use ToDictionary<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>) to create a Dictionary<TKey,TValue> by using a key selector.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
public long TrackingNumber { get; set; }
}
/*
This code produces the following output:
Remarks
The ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) method uses the default equality
comparer Default to compare keys.
ToDictionary<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IEqualityComparer<TKey>)
Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function and key
comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the keys returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to create a Dictionary<TKey,TValue> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Collections.Generic.Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
Exceptions
ArgumentNullException
source or keySelector is null .
-or-
keySelector produces a key that is null .
ArgumentException
keySelector produces duplicate keys for two elements.
Remarks
If comparer is null , the default equality comparer Default is used to compare keys.
Enumerable.ToHashSet
I n this Article
Overloads
ToHashSet<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)
ToHashSet<TSource>(IEnumerable<TSource>)
ToHashSet<TSource>(IEnumerable<TSource>,
IEqualityComparer<TSource>)
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
Parameters
source System.Collections.Generic.IEnumerable<TSource>
comparer System.Collections.Generic.IEqualityComparer<TSource>
Returns
System.Collections.Generic.HashSet<TSource>
ToHashSet<TSource>(IEnumerable<TSource>)
public static System.Collections.Generic.HashSet<TSource> ToHashSet<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
Parameters
source System.Collections.Generic.IEnumerable<TSource>
Returns
System.Collections.Generic.HashSet<TSource>
Enumerable.ToList
I n this Article
Creates a List<T> from an IEnumerable<T>.
public static System.Collections.Generic.List<TSource> ToList<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a List<T> from.
Returns
System.Collections.Generic.List<TSource>
A List<T> that contains elements from the input sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use ToList to force immediate query evaluation and return a List<T>
that contains the query results.
string[] fruits = { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
/*
This code produces the following output:
5
12
6
5
6
9
5
10
*/
Remarks
The ToList<TSource>(IEnumerable<TSource>) method forces immediate query evaluation and returns a List<T> that
contains the query results. You can append this method to your query in order to obtain a cached copy of the query
results.
ToArray has similar behavior but returns an array instead of a List<T>.
Enumerable.ToLookup
I n this Article
Overloads
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) Creates a Lookup<TKey,TElement> from an IEnumerable<T>
according to specified key selector and element selector
functions.
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>, IEquality Creates a Lookup<TKey,TElement> from an IEnumerable<T>
Comparer<TKey>) according to a specified key selector function, a comparer and
an element selector function.
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>) Creates a Lookup<TKey,TElement> from an IEnumerable<T>
according to a specified key selector function.
ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<
TSource,TKey>, IEqualityComparer<TKey>) Creates a Lookup<TKey,TElement> from an IEnumerable<T>
according to a specified key selector function and key
comparer.
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>)
Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to specified key selector and element selector
functions.
public static System.Linq.ILookup<TKey,TElement> ToLookup<TSource,TKey,TElement> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
Func<TSource,TElement> elementSelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
ILookup<TKey,TElement>
A Lookup<TKey,TElement> that contains values of type TElement selected from the input sequence.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
Examples
The following code example demonstrates how to use ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) to create a Lookup<TKey,TElement> by using a key selector function
and an element selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
public long TrackingNumber { get; set; }
}
/*
This code produces the following output:
C
Coho Vineyard 89453312
Contoso Pharmaceuticals 670053128
L
Lucerne Publishing 89112755
W
Wingtip Toys 299456122
Wide World Importers 4665518773
*/
Remarks
The ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
method returns a Lookup<TKey,TElement>, a one-to-many dictionary that maps keys to collections of values. A
Lookup<TKey,TElement> differs from a Dictionary<TKey,TValue>, which performs a one-to-one mapping of keys to
single values.
The default equality comparer Default is used to compare keys.
ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to a specified key selector function, a comparer
and an element selector function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
elementSelector System.Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ILookup<TKey,TElement>
A Lookup<TKey,TElement> that contains values of type TElement selected from the input sequence.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
Remarks
The ToLookup<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>) method returns a Lookup<TKey,TElement>, a one-to-many dictionary that maps keys to
collections of values. A Lookup<TKey,TElement> differs from a Dictionary<TKey,TValue>, which performs a one-to-one
mapping of keys to single values.
If comparer is null , the default equality comparer Default is used to compare keys.
ToLookup<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>)
Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to a specified key selector function.
public static System.Linq.ILookup<TKey,TSource> ToLookup<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
Returns
System.Linq.ILookup<TKey,TSource>
A Lookup<TKey,TElement> that contains keys and values.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
The ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) method returns a
Lookup<TKey,TElement>, a one-to-many dictionary that maps keys to collections of values. A Lookup<TKey,TElement>
differs from a Dictionary<TKey,TValue>, which performs a one-to-one mapping of keys to single values.
The default equality comparer Default is used to compare keys.
ToLookup<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>, IEqualityComparer<TKey>)
Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to a specified key selector function and key
comparer.
public static System.Linq.ILookup<TKey,TSource> ToLookup<TSource,TKey> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
The IEnumerable<T> to create a Lookup<TKey,TElement> from.
keySelector System.Func<TSource,TKey>
A function to extract a key from each element.
comparer System.Collections.Generic.IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
System.Linq.ILookup<TKey,TSource>
A Lookup<TKey,TElement> that contains keys and values.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
The ToLookup<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) method
returns a Lookup<TKey,TElement>, a one-to-many dictionary that maps keys to collections of values. A
Lookup<TKey,TElement> is different to a Dictionary<TKey,TValue>, which performs a one-to-one mapping of keys to
single values.
If comparer is null , the default equality comparer Default is used to compare keys.
Enumerable.Union
I n this Article
Overloads
Union<TSource>(IEnumerable<TSource>, IEnumerable<
TSource>) Produces the set union of two sequences by using the default
equality comparer.
Union<TSource>(IEnumerable<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) Produces the set union of two sequences by using a specified
IEqualityComparer<T>.
Union<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>)
Produces the set union of two sequences by using the default equality comparer.
public static System.Collections.Generic.IEnumerable<TSource> Union<TSource> (this
System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the first set for the union.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the second set for the union.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following code example demonstrates how to use Union<TSource>(IEnumerable<TSource>,
IEnumerable<TSource>) to obtain the union of two sequences of integers.
int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };
/*
This code produces the following output:
5 3 9 7 8 6 4 1 0
*/
If you want to compare sequences of objects of a custom data type, you have to implement the IEqualityComparer<T>
generic interface in a helperclass. The following code example shows how to implement this interface in a custom data
type and provide GetHashCode and Equals methods.
After you implement this interface, you can use sequences of ProductA objects in the Union<TSource>
(IEnumerable<TSource>, IEnumerable<TSource>) method, as shown in the following example.
IEnumerable<ProductA> union =
store1.Union(store2);
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
This method excludes duplicates from the return set. This is different behavior to the Concat method, which returns all
the elements in the input sequences including duplicates.
The default equality comparer, Default, is used to compare values of the types that implement the
IEqualityComparer<T> generic interface. To compare a custom data type, you need to implement this interface and
provide your own GetHashCode and Equals methods for the type.
When the object returned by this method is enumerated, Union enumerates first and second in that order and
yields each element that has not already been yielded.
Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>,
IEqualityComparer<TSource>)
Produces the set union of two sequences by using a specified IEqualityComparer<T>.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the first set for the union.
second System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> whose distinct elements form the second set for the union.
comparer System.Collections.Generic.IEqualityComparer<TSource>
The IEqualityComparer<T> to compare values.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following example shows how to implement an equality comparer that can be used in the Union method.
public class Product
{
public string Name { get; set; }
public int Code { get; set; }
}
After you implement this comparer, you can use sequences of Product objects in the Union method, as shown in the
following example.
IEnumerable<Product> union =
store1.Union(store2, new ProductComparer());
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
If c omparer is null , the default equality comparer, Default, is used to compare values.
When the object returned by this method is enumerated, Union enumerates first and second in that order and
yields each element that has not already been yielded.
The Concat method differs from the Union method because the Concat method returns all the elements in the input
sequences including duplicates, whereas Union returns only unique values.
Enumerable.Where
I n this Article
Overloads
Where<TSource>(IEnumerable<TSource>, Func<TSource,
Int32,Boolean>) Filters a sequence of values based on a predicate. Each
element's index is used in the logic of the predicate function.
Where<TSource>(IEnumerable<TSource>, Func<TSource,
Boolean>) Filters a sequence of values based on a predicate.
Where<TSource>(IEnumerable<TSource>,
Func<TSource,Int32,Boolean>)
Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to filter.
predicate System.Func<TSource,System.Int32,System.Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the
source element.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use Where<TSource>(IEnumerable<TSource>,
Func<TSource,Int32,Boolean>) to filter a sequence based on a predicate that involves the index of each element.
int[] numbers = { 0, 30, 20, 15, 90, 85, 40, 75 };
IEnumerable<int> query =
numbers.Where((number, index) => number <= index * 10);
0
20
15
40
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The first argument of predicate represents the element to test. The second argument represents the zero-based index
of the element within source .
Where<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>)
Filters a sequence of values based on a predicate.
Type Parameters
TSource
The type of the elements of source .
Parameters
source System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> to filter.
predicate System.Func<TSource,System.Boolean>
A function to test each element for a condition.
Returns
System.Collections.Generic.IEnumerable<TSource>
An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use Where<TSource>(IEnumerable<TSource>,
Func<TSource,Boolean>) to filter a sequence.
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
apple
mango
grape
*/
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
In query expression syntax, a where (Visual C#) or Where (Visual Basic) clause translates to an invocation of
Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>).
Enumerable.Zip
I n this Article
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
public static System.Collections.Generic.IEnumerable<TResult> Zip<TFirst,TSecond,TResult> (this
System.Collections.Generic.IEnumerable<TFirst> first,
System.Collections.Generic.IEnumerable<TSecond> second, Func<TFirst,TSecond,TResult>
resultSelector);
Type Parameters
TFirst
The type of the elements of the first input sequence.
TSecond
The type of the elements of the second input sequence.
TResult
The type of the elements of the result sequence.
Parameters
first System.Collections.Generic.IEnumerable<TFirst>
The first sequence to merge.
second System.Collections.Generic.IEnumerable<TSecond>
The second sequence to merge.
resultSelector System.Func<TFirst,TSecond,TResult>
A function that specifies how to merge the elements from the two sequences.
Returns
System.Collections.Generic.IEnumerable<TResult>
An IEnumerable<T> that contains merged elements of two input sequences.
Exceptions
ArgumentNullException
first or second is null .
Examples
The following code example demonstrates how to use the Zip method to merge two sequences.
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
// 1 one
// 2 two
// 3 three
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the
information that is required to perform the action. The query represented by this method is not executed until the
object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or
For Each in Visual Basic.
The method merges each element of the first sequence with an element that has the same index in the second
sequence. If the sequences do not have the same number of elements, the method merges sequences until it reaches
the end of one of them. For example, if one sequence has three elements and the other one has four, the result
sequence will have only three elements.
EnumerableExecutor Class
Represents an expression tree and provides functionality to execute the expression tree after rewriting it.
D eclaration
public abstract class EnumerableExecutor
I nheritance H ierarchy
Object
Constructors
EnumerableExecutor()
D eclaration
public class EnumerableExecutor<T> : System.Linq.EnumerableExecutor
I nheritance H ierarchy
Object
EnumerableExecutor
Constructors
EnumerableExecutor<T>(Expression)
Parameters
expression Expression
An expression tree to associate with the new instance.
EnumerableExecutor<T>
I n this Article
Initializes a new instance of the EnumerableExecutor<T> class.
public EnumerableExecutor (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree to associate with the new instance.
EnumerableQuery Class
Represents an IEnumerable as an EnumerableQuery data source.
D eclaration
public abstract class EnumerableQuery
I nheritance H ierarchy
Object
Constructors
EnumerableQuery()
D eclaration
public class EnumerableQuery<T> : System.Linq.EnumerableQuery,
System.Collections.Generic.IEnumerable<T>, System.Linq.IOrderedQueryable<T>,
System.Linq.IQueryable<T>, System.Linq.IQueryProvider
I nheritance H ierarchy
Object
EnumerableQuery
Constructors
EnumerableQuery<T>(IEnumerable<T>)
Initializes a new instance of the EnumerableQuery<T> class and associates it with an IEnumerable<T> collection.
D eclaration
public EnumerableQuery (System.Collections.Generic.IEnumerable<T> enumerable);
Parameters
enumerable IEnumerable<T>
A collection to associate with the new instance.
EnumerableQuery<T>(Expression)
Initializes a new instance of the EnumerableQuery<T> class and associates the instance with an expression tree.
D eclaration
public EnumerableQuery (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree to associate with the new instance.
Methods
ToString()
Returns a textual representation of the enumerable collection or, if it is null, of the expression tree that is
associated with this instance.
D eclaration
public override string ToString ();
Returns
String
String
A textual representation of the enumerable collection or, if it is null, of the expression tree that is associated with this instance.
IEnumerable<T>.GetEnumerator()
Returns an enumerator that can iterate through the associated IEnumerable<T> collection, or, if it is null, through
the collection that results from rewriting the associated expression tree as a query on an IEnumerable<T> data
source and executing it.
D eclaration
System.Collections.Generic.IEnumerator<T> IEnumerable<T>.GetEnumerator ();
Returns
IEnumerator<T>
An enumerator that can be used to iterate through the associated data source.
IEnumerable.GetEnumerator()
Returns an enumerator that can iterate through the associated IEnumerable<T> collection, or, if it is null, through
the collection that results from rewriting the associated expression tree as a query on an IEnumerable<T> data
source and executing it.
D eclaration
System.Collections.IEnumerator IEnumerable.GetEnumerator ();
Returns
IEnumerator
An enumerator that can be used to iterate through the associated data source.
IQueryable.ElementType
Gets the type of the data in the collection that this instance represents.
D eclaration
Type System.Linq.IQueryable.ElementType { get; }
Returns
Type
The type of the data in the collection that this instance represents.
IQueryable.Expression
Gets the expression tree that is associated with or that represents this instance.
D eclaration
System.Linq.Expressions.Expression System.Linq.IQueryable.Expression { get; }
System.Linq.Expressions.Expression System.Linq.IQueryable.Expression { get; }
Returns
Expression
The expression tree that is associated with or that represents this instance.
IQueryable.Provider
Returns
IQueryProvider
The query provider that is associated with this instance.
IQueryProvider.CreateQuery(Expression)
Constructs a new EnumerableQuery<T> object and associates it with a specified expression tree that represents an
IQueryable collection of data.
D eclaration
System.Linq.IQueryable IQueryProvider.CreateQuery (System.Linq.Expressions.Expression
expression);
Parameters
expression Expression
An expression tree that represents an IQueryable collection of data.
Returns
IQueryable
An EnumerableQuery<T> object that is associated with expression .
IQueryProvider.CreateQuery<S>(Expression)
Constructs a new EnumerableQuery<T> object and associates it with a specified expression tree that represents an
IQueryable<T> collection of data.
D eclaration
System.Linq.IQueryable<S> IQueryProvider.CreateQuery<S> (System.Linq.Expressions.Expression
expression);
Parameters
expression Expression
An expression tree to execute.
Returns
System.Linq.IQueryable<S>
An EnumerableQuery object that is associated with expression .
IQueryProvider.Execute(Expression)
Executes an expression after rewriting it to call Enumerable methods instead of Queryable methods on any
enumerable data sources that cannot be queried by Queryable methods.
D eclaration
object IQueryProvider.Execute (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree to execute.
Returns
Object
The value that results from executing expression .
IQueryProvider.Execute<S>(Expression)
Executes an expression after rewriting it to call Enumerable methods instead of Queryable methods on any
enumerable data sources that cannot be queried by Queryable methods.
D eclaration
S IQueryProvider.Execute<S> (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree to execute.
Returns
S
The value that results from executing expression .
EnumerableQuery<T>
I n this Article
Overloads
EnumerableQuery<T>(IEnumerable<T>)
Initializes a new instance of the EnumerableQuery<T> class
and associates it with an IEnumerable<T> collection.
EnumerableQuery<T>(Expression)
Initializes a new instance of the EnumerableQuery<T> class
and associates the instance with an expression tree.
EnumerableQuery<T>(IEnumerable<T>)
Initializes a new instance of the EnumerableQuery<T> class and associates it with an IEnumerable<T> collection.
public EnumerableQuery (System.Collections.Generic.IEnumerable<T> enumerable);
Parameters
enumerable IEnumerable<T>
A collection to associate with the new instance.
EnumerableQuery<T>(Expression)
Initializes a new instance of the EnumerableQuery<T> class and associates the instance with an expression tree.
public EnumerableQuery (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree to associate with the new instance.
EnumerableQuery<T>.ToString
I n this Article
Returns a textual representation of the enumerable collection or, if it is null, of the expression tree that is associated
with this instance.
Returns
String
A textual representation of the enumerable collection or, if it is null, of the expression tree that is associated with this
instance.
IGrouping<TKey,TElement> Interface
Represents a collection of objects that have a common key.
D eclaration
public interface IGrouping<out TKey,out TElement> : System.Collections.Generic.IEnumerable<out
TElement>
I nheritance H ierarchy
None
Remarks
An IGrouping<TKey,TElement> is an IEnumerable<T> that additionally has a key. The key represents the attribute that
is common to each value in the IGrouping<TKey,TElement>.
The values of an IGrouping<TKey,TElement> are accessed much as the elements of an IEnumerable<T> are accessed.
For example, you can access the values by using a foreach in Visual C# or For Each in Visual Basic loop to iterate
through the IGrouping<TKey,TElement> object. The Example section contains a code example that shows you how to
access both the key and the values of an IGrouping<TKey,TElement> object.
The IGrouping<TKey,TElement> type is used by the GroupBy standard query operator methods, which return a
sequence of elements of type IGrouping<TKey,TElement>.
Properties
Key
P roperty Value
TKey
The key of the IGrouping<TKey,TElement>.
IGrouping<TKey,TElement>.Key
I n this Article
Gets the key of the IGrouping<TKey,TElement>.
public TKey Key { get; }
Returns
TKey
The key of the IGrouping<TKey,TElement>.
Examples
The following example demonstrates how to use the Key property to label each IGrouping<TKey,TElement> object in a
sequence of IGrouping<TKey,TElement> objects. The GroupBy<TSource,TKey>(IEnumerable<TSource>,
Func<TSource,TKey>) method is used to obtain a sequence of IGrouping<TKey,TElement> objects. The foreach in
Visual C# or For Each in Visual Basic loop then iterates through each IGrouping<TKey,TElement> object, outputting its
key and the number of values it contains.
// Get a sequence of IGrouping objects.
IEnumerable<IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo>> memberQuery =
typeof(String).GetMembers().
GroupBy(member => member.MemberType);
// Output the key of each IGrouping object and the count of values.
foreach (IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> group in
memberQuery)
Console.WriteLine("(Key) {0} (Member count) {1}", group.Key, group.Count());
Remarks
The key of an IGrouping<TKey,TElement> represents the attribute that is common to each value in the
IGrouping<TKey,TElement>.
ILookup<TKey,TElement> Interface
Defines an indexer, size property, and Boolean search method for data structures that map keys to IEnumerable<T>
sequences of values.
D eclaration
public interface ILookup<TKey,TElement> :
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>
I nheritance H ierarchy
None
Remarks
The type Lookup<TKey,TElement> implements the ILookup<TKey,TElement> interface.
The extension method ToLookup, which can be appended to the end of a LINQ query, returns an object of type
ILookup<TKey,TElement>.
Properties
Count
P roperty Value
Int32
The number of key/value collection pairs in the ILookup<TKey,TElement>.
Item(TKey)
Parameters
key TKey
The key of the desired sequence of values.
P roperty Value
System.Collections.Generic.IEnumerable<TElement>
The IEnumerable<T> sequence of values indexed by the specified key.
Methods
Contains(TKey)
Parameters
key TKey
The key to search for in the ILookup<TKey,TElement>.
Returns
Boolean
true if key is in the ILookup<TKey,TElement>; otherwise, false .
ILookup<TKey,TElement>.Contains
I n this Article
Determines whether a specified key exists in the ILookup<TKey,TElement>.
public bool Contains (TKey key);
Parameters
key TKey
The key to search for in the ILookup<TKey,TElement>.
Returns
Boolean
true if key is in the ILookup<TKey,TElement>; otherwise, false .
ILookup<TKey,TElement>.Count
I n this Article
Gets the number of key/value collection pairs in the ILookup<TKey,TElement>.
public int Count { get; }
Returns
Int32
The number of key/value collection pairs in the ILookup<TKey,TElement>.
ILookup<TKey,TElement>.Item
I n this Article
Gets the IEnumerable<T> sequence of values indexed by a specified key.
public System.Collections.Generic.IEnumerable<TElement> this[TKey key] { get; }
Parameters
key TKey
The key of the desired sequence of values.
Returns
System.Collections.Generic.IEnumerable<TElement>
The IEnumerable<T> sequence of values indexed by the specified key.
ImmutableArrayExtensions Class
LINQ extension method overrides that offer greater efficiency for ImmutableArray<T> than the standard LINQ
methods
NuGet package: System.Collections.Immutable (about immutable collections and how to install)
D eclaration
public static class ImmutableArrayExtensions
I nheritance H ierarchy
Object
Methods
Aggregate<T>(ImmutableArray<T>, Func<T,T,T>)
Parameters
immutableArray ImmutableArray<T>
The collection to apply the function to.
func Func<T,T,T>
A function to be invoked on each element, in a cumulative way.
Returns
T
The final value after the cumulative function has been applied to all elements.
Parameters
immutableArray ImmutableArray<T>
The collection to apply the function to.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,T,TAccumulate>
func Func<TAccumulate,T,TAccumulate>
A function to be invoked on each element, in a cumulative way.
Returns
TAccumulate
The final accumulator value.
Parameters
immutableArray ImmutableArray<T>
The collection to apply the function to.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,T,TAccumulate>
A function to be invoked on each element, in a cumulative way.
resultSelector Func<TAccumulate,TResult>
Returns
TResult
The final accumulator value.
All<T>(ImmutableArray<T>, Func<T,Boolean>)
Gets a value indicating whether all elements in this array match a given condition.
D eclaration
public static bool All<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Parameters
immutableArray ImmutableArray<T>
The array to check for matches.
predicate Func<T,Boolean>
The predicate.
Returns
Boolean
Boolean
true if every element of the source sequence passes the test in the specified predicate; otherwise, false .
Any<T>(ImmutableArray<T>)
Parameters
immutableArray ImmutableArray<T>
The array to check for elements.
Returns
Boolean
true if the array contains an elements; otherwise, false .
Any<T>(ImmutableArray<T>)
Parameters
builder ImmutableArray<T>
The builder to check for matches.
Returns
Boolean
true if the array builder contains any elements; otherwise, false .
Any<T>(ImmutableArray<T>, Func<T,Boolean>)
Gets a value indicating whether the array contains any elements that match a specified condition.
D eclaration
public static bool Any<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Parameters
immutableArray ImmutableArray<T>
The array to check for elements.
predicate Func<T,Boolean>
The delegate that defines the condition to match to an element.
Returns
Boolean
true if an element matches the specified condition; otherwise, false .
ElementAt<T>(ImmutableArray<T>, Int32)
Parameters
immutableArray ImmutableArray<T>
The array to find an element in.
index Int32
The index for the element to retrieve.
Returns
T
The item at the specified index.
ElementAtOrDefault<T>(ImmutableArray<T>, Int32)
Returns the element at a specified index in a sequence or a default value if the index is out of range.
D eclaration
public static T ElementAtOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, int index);
Parameters
immutableArray ImmutableArray<T>
The array to find an element in.
index Int32
The index for the element to retrieve.
Returns
T
The item at the specified index, or the default value if the index is not found.
First<T>(ImmutableArray<T>)
Returns the first element in an array.
D eclaration
public static T First<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray);
Parameters
immutableArray ImmutableArray<T>
The array to get an item from.
Returns
T
The first item in the array.
First<T>(ImmutableArray<T>)
Parameters
builder ImmutableArray<T>
The builder to retrieve an item from.
Returns
T
The first item in the list.
First<T>(ImmutableArray<T>, Func<T,Boolean>)
Parameters
immutableArray ImmutableArray<T>
The array to get an item from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to search for.
Returns
T
The first item in the list if it meets the condition specified by predicate .
FirstOrDefault<T>(ImmutableArray<T>)
Returns the first element of a sequence, or a default value if the sequence contains no elements.
D eclaration
public static T FirstOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray);
Parameters
immutableArray ImmutableArray<T>
The array to retrieve items from.
Returns
T
The first item in the list, if found; otherwise the default value for the item type.
FirstOrDefault<T>(ImmutableArray<T>)
Returns the first element in the collection, or the default value if the collection is empty.
D eclaration
public static T FirstOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>.Builder
builder);
Parameters
builder ImmutableArray<T>
The builder to retrieve an element from.
Returns
T
The first item in the list, if found; otherwise the default value for the item type.
FirstOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
D eclaration
public static T FirstOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, Func<T,bool> predicate);
Parameters
immutableArray ImmutableArray<T>
The array to retrieve elments from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to search for.
Returns
T
T
The first item in the list, if found; otherwise the default value for the item type.
Last<T>(ImmutableArray<T>)
Parameters
builder ImmutableArray<T>
The builder to retrieve elements from.
Returns
T
The last element in the builder.
Last<T>(ImmutableArray<T>, Func<T,Boolean>)
Parameters
immutableArray ImmutableArray<T>
The array to retrieve elements from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to retrieve.
Returns
T
The last element of the array that satisfies the predicate condition.
Last<T>(ImmutableArray<T>)
Parameters
immutableArray ImmutableArray<T>
The array to retrieve items from.
Returns
T
The last element in the array.
LastOrDefault<T>(ImmutableArray<T>)
Returns the last element of a sequence, or a default value if the sequence contains no elements.
D eclaration
public static T LastOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray);
Parameters
immutableArray ImmutableArray<T>
The array to retrieve items from.
Returns
T
The last element of a sequence, or a default value if the sequence contains no elements.
LastOrDefault<T>(ImmutableArray<T>)
Returns the last element in the collection, or the default value if the collection is empty.
D eclaration
public static T LastOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>.Builder
builder);
Parameters
builder ImmutableArray<T>
The builder to retrieve an element from.
Returns
T
The last element of a sequence, or a default value if the sequence contains no elements.
LastOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
D eclaration
public static T LastOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, Func<T,bool> predicate);
Parameters
immutableArray ImmutableArray<T>
The array to retrieve an element from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to search for.
Returns
T
The last element of a sequence, or a default value if the sequence contains no elements.
Select<T,TResult>(ImmutableArray<T>, Func<T,TResult>)
Parameters
immutableArray ImmutableArray<T>
The immutable array to select items from.
selector Func<T,TResult>
A transform function to apply to each element.
Returns
IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.
SelectMany<TSource,TCollection,TResult>(ImmutableArray<TSource>,
Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence,
and invokes a result selector function on each element therein.
D eclaration
public static System.Collections.Generic.IEnumerable<TResult>
SelectMany<TSource,TCollection,TResult> (this
System.Collections.Immutable.ImmutableArray<TSource> immutableArray,
Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector,
Func<TSource,TCollection,TResult> resultSelector);
Parameters
immutableArray ImmutableArray<TSource>
The immutable array.
collectionSelector Func<TSource,IEnumerable<TCollection>>
A transform function to apply to each element of the input sequence.
resultSelector Func<TSource,TCollection,TResult>
A transform function to apply to each element of the intermediate sequence.
Returns
IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function collectionSelector on each
element of immutableArray and then mapping each of those sequence elements and their corresponding source element to a result
element.
Parameters
immutableArray ImmutableArray<TBase>
The array to use for comparison.
items IEnumerable<TDerived>
The items to use for comparison.
comparer IEqualityComparer<TBase>
The comparer to use to check for equality.
Returns
Boolean
true to indicate the sequences are equal; otherwise, false .
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>, ImmutableArray<TDerived>,
IEqualityComparer<TBase>)
Parameters
immutableArray ImmutableArray<TBase>
The array to use for comparison.
items ImmutableArray<TDerived>
The items to use for comparison.
comparer IEqualityComparer<TBase>
The comparer to use to check for equality.
Returns
Boolean
true to indicate the sequences are equal; otherwise, false .
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>, ImmutableArray<TDerived>,
Func<TBase,TBase,Boolean>)
Parameters
immutableArray ImmutableArray<TBase>
The array to use for comparison.
items ImmutableArray<TDerived>
The items to use for comparison.
predicate Func<TBase,TBase,Boolean>
The comparer to use to check for equality.
Returns
Boolean
true to indicate the sequences are equal; otherwise, false .
Single<T>(ImmutableArray<T>)
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the
sequence.
D eclaration
public static T Single<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray);
Parameters
immutableArray ImmutableArray<T>
The array to retrieve the element from.
Returns
T
T
The element in the sequence.
Single<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
one such element exists.
D eclaration
public static T Single<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Parameters
immutableArray ImmutableArray<T>
predicate Func<T,Boolean>
Returns
T
Returns Boolean.
SingleOrDefault<T>(ImmutableArray<T>)
Returns the only element of the array, or a default value if the sequence is empty; this method throws an exception
if there is more than one element in the sequence.
D eclaration
public static T SingleOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray);
Parameters
immutableArray ImmutableArray<T>
Returns
T
The element in the array, or the default value if the array is empty.
SingleOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element
exists; this method throws an exception if more than one element satisfies the condition.
D eclaration
public static T SingleOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, Func<T,bool> predicate);
Parameters
immutableArray ImmutableArray<T>
The array to get the element from.
predicate Func<T,Boolean>
The condition the element must satisfy.
Returns
T
The element if it satisfies the specified condition; otherwise the default element.
ToArray<T>(ImmutableArray<T>)
Parameters
immutableArray ImmutableArray<T>
Returns
T[]
The newly instantiated array.
ToDictionary<TKey,T>(ImmutableArray<T>, Func<T,TKey>)
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
Returns
Dictionary<TKey,T>
The newly initialized dictionary.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
comparer IEqualityComparer<TKey>
The comparer to initialize the dictionary with.
Returns
Dictionary<TKey,T>
The newly initialized dictionary.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
elementSelector Func<T,TElement>
The element selector.
comparer IEqualityComparer<TKey>
The comparer to initialize the dictionary with.
Returns
Dictionary<TKey,TElement>
The newly initialized dictionary.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
elementSelector Func<T,TElement>
The element selector.
Returns
Dictionary<TKey,TElement>
The newly initialized dictionary.
Where<T>(ImmutableArray<T>, Func<T,Boolean>)
Parameters
immutableArray ImmutableArray<T>
The array to filter.
predicate Func<T,Boolean>
The condition to use for filtering the array content.
Returns
IEnumerable<T>
Returns IEnumerable<T> that contains elements that meet the condition.
ImmutableArrayExtensions.Aggregate
I n this Article
Overloads
Aggregate<T>(ImmutableArray<T>, Func<T,T,T>)
Applies a function to a sequence of elements in a cumulative
way.
Aggregate<TAccumulate,T>(ImmutableArray<T>,
TAccumulate, Func<TAccumulate,T,TAccumulate>) Applies a function to a sequence of elements in a cumulative
way.
Aggregate<TAccumulate,TResult,T>(ImmutableArray<T>,
TAccumulate, Func<TAccumulate,T,TAccumulate>, Func< Applies a function to a sequence of elements in a cumulative
TAccumulate,TResult>) way.
Aggregate<T>(ImmutableArray<T>, Func<T,T,T>)
Applies a function to a sequence of elements in a cumulative way.
public static T Aggregate<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,T,T> func);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The collection to apply the function to.
func Func<T,T,T>
A function to be invoked on each element, in a cumulative way.
Returns
T
The final value after the cumulative function has been applied to all elements.
Remarks
Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling
func one time for each element in source. Each time func is called, Aggregate passes both the element from the
sequence and an aggregated value (as the first argument to func). The value of the seed parameter is used as the initial
aggregate value. The result of func replaces the previous aggregated value. Aggregate returns the final result of func .
Aggregate<TAccumulate,T>(ImmutableArray<T>, TAccumulate,
Func<TAccumulate,T,TAccumulate>)
Applies a function to a sequence of elements in a cumulative way.
public static TAccumulate Aggregate<TAccumulate,T> (this
System.Collections.Immutable.ImmutableArray<T> immutableArray, TAccumulate seed,
Func<TAccumulate,T,TAccumulate> func);
Type Parameters
TAccumulate
The type of the accumulated value.
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The collection to apply the function to.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,T,TAccumulate>
A function to be invoked on each element, in a cumulative way.
Returns
TAccumulate
The final accumulator value.
Remarks
Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling
func one time for each element in source. Each time func is called, Aggregate passes both the element from the
sequence and an aggregated value (as the first argument to func). The value of the seed parameter is used as the initial
aggregate value. The result of func replaces the previous aggregated value. Aggregate returns the final result of func .
Aggregate<TAccumulate,TResult,T>(ImmutableArray<T>,
TAccumulate, Func<TAccumulate,T,TAccumulate>,
Func<TAccumulate,TResult>)
Applies a function to a sequence of elements in a cumulative way.
Type Parameters
TAccumulate
The type of the accumulated value.
TResult
The type of result returned by the result selector.
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The collection to apply the function to.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,T,TAccumulate>
A function to be invoked on each element, in a cumulative way.
resultSelector Func<TAccumulate,TResult>
Returns
TResult
The final accumulator value.
Remarks
Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling
func one time for each element in source. Each time func is called, Aggregate passes both the element from the
sequence and an aggregated value (as the first argument to func). The value of the seed parameter is used as the initial
aggregate value. The result of func replaces the previous aggregated value. Aggregate returns the final result of func .
ImmutableArrayExtensions.All
I n this Article
Gets a value indicating whether all elements in this array match a given condition.
public static bool All<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to check for matches.
predicate Func<T,Boolean>
The predicate.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate; otherwise, false .
ImmutableArrayExtensions.Any
I n this Article
Overloads
Any<T>(ImmutableArray<T>)
Gets a value indicating whether the array contains any
elements.
Any<T>(ImmutableArray<T>)
Returns a value indicating whether this collection contains any
elements.
Any<T>(ImmutableArray<T>, Func<T,Boolean>)
Gets a value indicating whether the array contains any
elements that match a specified condition.
Any<T>(ImmutableArray<T>)
Gets a value indicating whether the array contains any elements.
public static bool Any<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to check for elements.
Returns
Boolean
true if the array contains an elements; otherwise, false .
Any<T>(ImmutableArray<T>)
Returns a value indicating whether this collection contains any elements.
public static bool Any<T> (this System.Collections.Immutable.ImmutableArray<T>.Builder builder);
Type Parameters
T
The type of elements in the array.
Parameters
builder ImmutableArray<T>
The builder to check for matches.
Returns
Boolean
true if the array builder contains any elements; otherwise, false .
Any<T>(ImmutableArray<T>, Func<T,Boolean>)
Gets a value indicating whether the array contains any elements that match a specified condition.
public static bool Any<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to check for elements.
predicate Func<T,Boolean>
The delegate that defines the condition to match to an element.
Returns
Boolean
true if an element matches the specified condition; otherwise, false .
ImmutableArrayExtensions.ElementAt
I n this Article
Returns the element at a specified index in the array.
public static T ElementAt<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
int index);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to find an element in.
index Int32
The index for the element to retrieve.
Returns
T
The item at the specified index.
ImmutableArrayExtensions.ElementAtOrDefault
I n this Article
Returns the element at a specified index in a sequence or a default value if the index is out of range.
public static T ElementAtOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, int index);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to find an element in.
index Int32
The index for the element to retrieve.
Returns
T
The item at the specified index, or the default value if the index is not found.
ImmutableArrayExtensions.First
I n this Article
Overloads
First<T>(ImmutableArray<T>)
Returns the first element in an array.
First<T>(ImmutableArray<T>)
Returns the first element in the collection.
First<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the first element in a sequence that satisfies a
specified condition.
First<T>(ImmutableArray<T>)
Returns the first element in an array.
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to get an item from.
Returns
T
The first item in the array.
Exceptions
InvalidOperationException
If the array is empty.
First<T>(ImmutableArray<T>)
Returns the first element in the collection.
public static T First<T> (this System.Collections.Immutable.ImmutableArray<T>.Builder builder);
Type Parameters
T
The type of items in the array.
Parameters
builder ImmutableArray<T>
The builder to retrieve an item from.
Returns
T
The first item in the list.
Exceptions
InvalidOperationException
If the array is empty.
First<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the first element in a sequence that satisfies a specified condition.
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to get an item from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to search for.
Returns
T
The first item in the list if it meets the condition specified by predicate .
Exceptions
InvalidOperationException
If the array is empty.
ImmutableArrayExtensions.FirstOrDefault
I n this Article
Overloads
FirstOrDefault<T>(ImmutableArray<T>)
Returns the first element of a sequence, or a default value if
the sequence contains no elements.
FirstOrDefault<T>(ImmutableArray<T>)
Returns the first element in the collection, or the default value
if the collection is empty.
FirstOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the first element of the sequence that satisfies a
condition or a default value if no such element is found.
FirstOrDefault<T>(ImmutableArray<T>)
Returns the first element of a sequence, or a default value if the sequence contains no elements.
public static T FirstOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve items from.
Returns
T
The first item in the list, if found; otherwise the default value for the item type.
FirstOrDefault<T>(ImmutableArray<T>)
Returns the first element in the collection, or the default value if the collection is empty.
public static T FirstOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>.Builder
builder);
Type Parameters
T
The type of item in the builder.
Parameters
builder ImmutableArray<T>
The builder to retrieve an element from.
Returns
T
The first item in the list, if found; otherwise the default value for the item type.
FirstOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
public static T FirstOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve elments from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to search for.
Returns
T
The first item in the list, if found; otherwise the default value for the item type.
ImmutableArrayExtensions.Last
I n this Article
Overloads
Last<T>(ImmutableArray<T>)
Returns the last element in the collection.
Last<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the last element of a sequence that satisfies a
specified condition.
Last<T>(ImmutableArray<T>)
Returns the last element of the array.
Last<T>(ImmutableArray<T>)
Returns the last element in the collection.
Type Parameters
T
The type of item in the builder.
Parameters
builder ImmutableArray<T>
The builder to retrieve elements from.
Returns
T
The last element in the builder.
Exceptions
InvalidOperationException
Thrown if the collection is empty.
Last<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the last element of a sequence that satisfies a specified condition.
public static T Last<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve elements from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to retrieve.
Returns
T
The last element of the array that satisfies the predicate condition.
Exceptions
InvalidOperationException
Thrown if the collection is empty.
Last<T>(ImmutableArray<T>)
Returns the last element of the array.
Type Parameters
T
The type of element contained by the array.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve items from.
Returns
T
The last element in the array.
Exceptions
InvalidOperationException
Thrown if the collection is empty.
ImmutableArrayExtensions.LastOrDefault
I n this Article
Overloads
LastOrDefault<T>(ImmutableArray<T>)
Returns the last element of a sequence, or a default value if
the sequence contains no elements.
LastOrDefault<T>(ImmutableArray<T>)
Returns the last element in the collection, or the default value
if the collection is empty.
LastOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the last element of a sequence that satisfies a
condition or a default value if no such element is found.
LastOrDefault<T>(ImmutableArray<T>)
Returns the last element of a sequence, or a default value if the sequence contains no elements.
public static T LastOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve items from.
Returns
T
The last element of a sequence, or a default value if the sequence contains no elements.
LastOrDefault<T>(ImmutableArray<T>)
Returns the last element in the collection, or the default value if the collection is empty.
public static T LastOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>.Builder
builder);
Type Parameters
T
The type of item in the builder.
Parameters
builder ImmutableArray<T>
The builder to retrieve an element from.
Returns
T
The last element of a sequence, or a default value if the sequence contains no elements.
LastOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
public static T LastOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve an element from.
predicate Func<T,Boolean>
The delegate that defines the conditions of the element to search for.
Returns
T
The last element of a sequence, or a default value if the sequence contains no elements.
ImmutableArrayExtensions.Select
I n this Article
Projects each element of a sequence into a new form.
public static System.Collections.Generic.IEnumerable<TResult> Select<T,TResult> (this
System.Collections.Immutable.ImmutableArray<T> immutableArray, Func<T,TResult> selector);
Type Parameters
T
The type of element contained by the collection.
TResult
The type of the result element.
Parameters
immutableArray ImmutableArray<T>
The immutable array to select items from.
selector Func<T,TResult>
A transform function to apply to each element.
Returns
IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.
ImmutableArrayExtensions.SelectMany
I n this Article
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein.
Type Parameters
TSource
The type of the elements of immutableArray .
TCollection
The type of the intermediate elements collected by collectionSelector .
TResult
The type of the elements of the resulting sequence.
Parameters
immutableArray ImmutableArray<TSource>
The immutable array.
collectionSelector Func<TSource,IEnumerable<TCollection>>
A transform function to apply to each element of the input sequence.
resultSelector Func<TSource,TCollection,TResult>
A transform function to apply to each element of the intermediate sequence.
Returns
IEnumerable<TResult>
An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function
collectionSelector on each element of immutableArray and then mapping each of those sequence elements and
their corresponding source element to a result element.
ImmutableArrayExtensions.SequenceEqual
I n this Article
Overloads
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>,
IEnumerable<TDerived>, IEqualityComparer<TBase>) Determines whether two sequences are equal according to an
equality comparer.
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>,
ImmutableArray<TDerived>, IEqualityComparer<TBase>) Determines whether two sequences are equal according to an
equality comparer.
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>,
ImmutableArray<TDerived>, Func<TBase,TBase,Boolean>) Determines whether two sequences are equal according to an
equality comparer.
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>,
IEnumerable<TDerived>, IEqualityComparer<TBase>)
Determines whether two sequences are equal according to an equality comparer.
public static bool SequenceEqual<TDerived,TBase> (this
System.Collections.Immutable.ImmutableArray<TBase> immutableArray,
System.Collections.Generic.IEnumerable<TDerived> items,
System.Collections.Generic.IEqualityComparer<TBase> comparer = null) where TDerived : TBase;
Type Parameters
TDerived
The type of element in the compared array.
TBase
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<TBase>
The array to use for comparison.
items IEnumerable<TDerived>
The items to use for comparison.
comparer IEqualityComparer<TBase>
The comparer to use to check for equality.
Returns
Boolean
true to indicate the sequences are equal; otherwise, false .
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>,
ImmutableArray<TDerived>, IEqualityComparer<TBase>)
Determines whether two sequences are equal according to an equality comparer.
public static bool SequenceEqual<TDerived,TBase> (this
System.Collections.Immutable.ImmutableArray<TBase> immutableArray,
System.Collections.Immutable.ImmutableArray<TDerived> items,
System.Collections.Generic.IEqualityComparer<TBase> comparer = null) where TDerived : TBase;
Type Parameters
TDerived
The type of element in the compared array.
TBase
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<TBase>
The array to use for comparison.
items ImmutableArray<TDerived>
The items to use for comparison.
comparer IEqualityComparer<TBase>
The comparer to use to check for equality.
Returns
Boolean
true to indicate the sequences are equal; otherwise, false .
SequenceEqual<TDerived,TBase>(ImmutableArray<TBase>,
ImmutableArray<TDerived>, Func<TBase,TBase,Boolean>)
Determines whether two sequences are equal according to an equality comparer.
public static bool SequenceEqual<TDerived,TBase> (this
System.Collections.Immutable.ImmutableArray<TBase> immutableArray,
System.Collections.Immutable.ImmutableArray<TDerived> items, Func<TBase,TBase,bool> predicate) where
TDerived : TBase;
Type Parameters
TDerived
The type of element in the compared array.
TBase
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<TBase>
The array to use for comparison.
items ImmutableArray<TDerived>
The items to use for comparison.
predicate Func<TBase,TBase,Boolean>
The comparer to use to check for equality.
Returns
Boolean
true to indicate the sequences are equal; otherwise, false .
ImmutableArrayExtensions.Single
I n this Article
Overloads
Single<T>(ImmutableArray<T>)
Returns the only element of a sequence, and throws an
exception if there is not exactly one element in the sequence.
Single<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the only element of a sequence that satisfies a
specified condition, and throws an exception if more than one
such element exists.
Single<T>(ImmutableArray<T>)
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
public static T Single<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to retrieve the element from.
Returns
T
The element in the sequence.
Single<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one
such element exists.
public static T Single<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray,
Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
predicate Func<T,Boolean>
Returns
T
Returns Boolean.
ImmutableArrayExtensions.SingleOrDefault
I n this Article
Overloads
SingleOrDefault<T>(ImmutableArray<T>)
Returns the only element of the array, or a default value if the
sequence is empty; this method throws an exception if there is
more than one element in the sequence.
SingleOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the only element of a sequence that satisfies a
specified condition or a default value if no such element exists;
this method throws an exception if more than one element
satisfies the condition.
SingleOrDefault<T>(ImmutableArray<T>)
Returns the only element of the array, or a default value if the sequence is empty; this method throws an exception if
there is more than one element in the sequence.
public static T SingleOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
Returns
T
The element in the array, or the default value if the array is empty.
SingleOrDefault<T>(ImmutableArray<T>, Func<T,Boolean>)
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists;
this method throws an exception if more than one element satisfies the condition.
public static T SingleOrDefault<T> (this System.Collections.Immutable.ImmutableArray<T>
immutableArray, Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to get the element from.
predicate Func<T,Boolean>
The condition the element must satisfy.
Returns
T
The element if it satisfies the specified condition; otherwise the default element.
ImmutableArrayExtensions.ToArray
I n this Article
Copies the contents of this array to a mutable array.
public static T[] ToArray<T> (this System.Collections.Immutable.ImmutableArray<T> immutableArray);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
Returns
T[]
The newly instantiated array.
ImmutableArrayExtensions.ToDictionary
I n this Article
Overloads
ToDictionary<TKey,T>(ImmutableArray<T>, Func<T,TKey>)
Creates a dictionary based on the contents of this array.
ToDictionary<TKey,T>(ImmutableArray<T>, Func<T,TKey>,
IEqualityComparer<TKey>) Creates a dictionary based on the contents of this array.
ToDictionary<TKey,TElement,T>(ImmutableArray<T>, Func<T,
TKey>, Func<T,TElement>, IEqualityComparer<TKey>) Creates a dictionary based on the contents of this array.
ToDictionary<TKey,TElement,T>(ImmutableArray<T>, Func<T,
TKey>, Func<T,TElement>) Creates a dictionary based on the contents of this array.
ToDictionary<TKey,T>(ImmutableArray<T>, Func<T,TKey>)
Creates a dictionary based on the contents of this array.
public static System.Collections.Generic.Dictionary<TKey,T> ToDictionary<TKey,T> (this
System.Collections.Immutable.ImmutableArray<T> immutableArray, Func<T,TKey> keySelector);
Type Parameters
TKey
The type of the key.
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
Returns
Dictionary<TKey,T>
The newly initialized dictionary.
ToDictionary<TKey,T>(ImmutableArray<T>, Func<T,TKey>,
IEqualityComparer<TKey>)
Creates a dictionary based on the contents of this array.
public static System.Collections.Generic.Dictionary<TKey,T> ToDictionary<TKey,T> (this
System.Collections.Immutable.ImmutableArray<T> immutableArray, Func<T,TKey> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TKey
The type of the key.
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
comparer IEqualityComparer<TKey>
The comparer to initialize the dictionary with.
Returns
Dictionary<TKey,T>
The newly initialized dictionary.
ToDictionary<TKey,TElement,T>(ImmutableArray<T>,
Func<T,TKey>, Func<T,TElement>, IEqualityComparer<TKey>)
Creates a dictionary based on the contents of this array.
public static System.Collections.Generic.Dictionary<TKey,TElement> ToDictionary<TKey,TElement,T>
(this System.Collections.Immutable.ImmutableArray<T> immutableArray, Func<T,TKey> keySelector,
Func<T,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TKey
The type of the key.
TElement
The type of the element.
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
elementSelector Func<T,TElement>
The element selector.
comparer IEqualityComparer<TKey>
The comparer to initialize the dictionary with.
Returns
Dictionary<TKey,TElement>
The newly initialized dictionary.
ToDictionary<TKey,TElement,T>(ImmutableArray<T>,
Func<T,TKey>, Func<T,TElement>)
Creates a dictionary based on the contents of this array.
Type Parameters
TKey
The type of the key.
TElement
The type of the element.
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to create a dictionary from.
keySelector Func<T,TKey>
The key selector.
elementSelector Func<T,TElement>
The element selector.
Returns
Dictionary<TKey,TElement>
The newly initialized dictionary.
ImmutableArrayExtensions.Where
I n this Article
Filters a sequence of values based on a predicate.
public static System.Collections.Generic.IEnumerable<T> Where<T> (this
System.Collections.Immutable.ImmutableArray<T> immutableArray, Func<T,bool> predicate);
Type Parameters
T
The type of element contained by the collection.
Parameters
immutableArray ImmutableArray<T>
The array to filter.
predicate Func<T,Boolean>
The condition to use for filtering the array content.
Returns
IEnumerable<T>
Returns IEnumerable<T> that contains elements that meet the condition.
IOrderedEnumerable<TElement> Interface
Represents a sorted sequence.
D eclaration
public interface IOrderedEnumerable<TElement> : System.Collections.Generic.IEnumerable<TElement>
I nheritance H ierarchy
None
Remarks
This type is enumerable because it inherits from IEnumerable<T>.
The extension methods ThenBy and ThenByDescending operate on objects of type IOrderedEnumerable<TElement>.
An object of type IOrderedEnumerable<TElement> can be obtained by calling one of the primary sort methods,
OrderBy or OrderByDescending, which return an IOrderedEnumerable<TElement>. ThenBy and ThenByDescending, the
subordinate sort methods, in turn also return an object of type IOrderedEnumerable<TElement>. This design allows for
any number of consecutive calls to ThenBy or ThenByDescending, where each call performs a subordinate ordering on
the sorted data returned from the previous call.
Methods
CreateOrderedEnumerable<TKey>(Func<TElement,TKey>, IComparer<TKey>, Boolean)
Parameters
keySelector Func<TElement,TKey>
The Func<T,TResult> used to extract the key for each element.
comparer IComparer<TKey>
The IComparer<T> used to compare keys for placement in the returned sequence.
descending Boolean
true to sort the elements in descending order; false to sort the elements in ascending order.
Returns
IOrderedEnumerable<TElement>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
IOrderedEnumerable<TElement>.CreateOrdered
Enumerable
I n this Article
Performs a subsequent ordering on the elements of an IOrderedEnumerable<TElement> according to a key.
Type Parameters
TKey
The type of the key produced by keySelector .
Parameters
keySelector Func<TElement,TKey>
The Func<T,TResult> used to extract the key for each element.
comparer IComparer<TKey>
The IComparer<T> used to compare keys for placement in the returned sequence.
descending Boolean
true to sort the elements in descending order; false to sort the elements in ascending order.
Returns
IOrderedEnumerable<TElement>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.
Examples
The following code example demonstrates how to use CreateOrderedEnumerable to perform a secondary ordering on
an IOrderedEnumerable<TElement>.
// Create an array of strings to sort.
string[] fruits = { "apricot", "orange", "banana", "mango", "apple", "grape", "strawberry" };
// First sort the strings by their length.
IOrderedEnumerable<string> sortedFruits2 =
fruits.OrderBy(fruit => fruit.Length);
// Secondarily sort the strings alphabetically, using the default comparer.
IOrderedEnumerable<string> sortedFruits3 =
sortedFruits2.CreateOrderedEnumerable<string>(
fruit => fruit,
Comparer<string>.Default, false);
Remarks
The functionality provided by this method is like that provided by ThenBy or ThenByDescending, depending on whether
descending is true or false . They both perform a subordinate ordering of an already sorted sequence of type
IOrderedEnumerable<TElement>.
IOrderedQueryable Interface
Represents the result of a sorting operation.
D eclaration
public interface IOrderedQueryable : System.Collections.IEnumerable, System.Linq.IQueryable
I nheritance H ierarchy
None
Remarks
The IOrderedQueryable interface is intended for implementation by query providers.
This interface represents the result of a sorting query that calls the method(s) OrderBy, OrderByDescending, ThenBy or
ThenByDescending. When CreateQuery is called and passed an expression tree that represents a sorting query, the
resulting IQueryable object must be of a type that implements IOrderedQueryable.
For more information about how to create your own LINQ provider, see LINQ: Building an IQueryable Provider on
MSDN Blogs.
IOrderedQueryable<T> Interface
Represents the result of a sorting operation.
D eclaration
public interface IOrderedQueryable<out T> : System.Collections.Generic.IEnumerable<out T>,
System.Linq.IOrderedQueryable, System.Linq.IQueryable<out T>
I nheritance H ierarchy
None
Remarks
The IOrderedQueryable<T> interface is intended for implementation by query providers.
This interface represents the result of a sorting query that calls the method(s) OrderBy, OrderByDescending, ThenBy or
ThenByDescending. When CreateQuery<TElement>(Expression) is called and passed an expression tree that represents
a sorting query, the resulting IQueryable<T> object must be of a type that implements IOrderedQueryable<T>.
For more information about how to create your own LINQ provider, see LINQ: Building an IQueryable Provider on
MSDN Blogs.
IQueryable Interface
Provides functionality to evaluate queries against a specific data source wherein the type of the data is not specified.
D eclaration
public interface IQueryable : System.Collections.IEnumerable
I nheritance H ierarchy
None
Remarks
The IQueryable interface is intended for implementation by query providers. It is only supposed to be implemented by
providers that also implement IQueryable<T>. If the provider does not also implement IQueryable<T>, the standard
query operators cannot be used on the provider's data source.
The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can
be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The
definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating the
expression tree to an appropriate query language for the underlying data source. Queries that do not return
enumerable results are executed when the Execute method is called.
For more information about how to create your own LINQ provider, see LINQ: Building an IQueryable Provider on
MSDN Blogs.
Properties
ElementType
Gets the type of the element(s) that are returned when the expression tree associated with this instance of
IQueryable is executed.
D eclaration
public Type ElementType { get; }
P roperty Value
Type
A Type that represents the type of the element(s) that are returned when the expression tree associated with this object is executed.
Expression
Gets the expression tree that is associated with the instance of IQueryable.
D eclaration
public System.Linq.Expressions.Expression Expression { get; }
P roperty Value
Expression
The Expression that is associated with this instance of IQueryable.
Provider
Gets the query provider that is associated with this data source.
D eclaration
public System.Linq.IQueryProvider Provider { get; }
P roperty Value
IQueryProvider
The IQueryProvider that is associated with this data source.
IQueryable.ElementType
I n this Article
Gets the type of the element(s) that are returned when the expression tree associated with this instance of IQueryable is
executed.
Returns
Type
A Type that represents the type of the element(s) that are returned when the expression tree associated with this object
is executed.
Remarks
The ElementType property represents the "T" in IQueryable<T> or IQueryable(Of T) .
IQueryable.Expression
I n this Article
Gets the expression tree that is associated with the instance of IQueryable.
public System.Linq.Expressions.Expression Expression { get; }
Returns
Expression
The Expression that is associated with this instance of IQueryable.
Remarks
If an instance of IQueryable represents a LINQ query against a data source, the associated expression tree represents
that query.
IQueryable.Provider
I n this Article
Gets the query provider that is associated with this data source.
public System.Linq.IQueryProvider Provider { get; }
Returns
IQueryProvider
The IQueryProvider that is associated with this data source.
Remarks
If an instance of IQueryable represents a LINQ query against a data source, the associated query provider is the
provider that created the IQueryable instance.
IQueryable<T> Interface
Provides functionality to evaluate queries against a specific data source wherein the type of the data is known.
D eclaration
public interface IQueryable<out T> : System.Collections.Generic.IEnumerable<out T>,
System.Linq.IQueryable
I nheritance H ierarchy
None
Remarks
The IQueryable<T> interface is intended for implementation by query providers.
This interface inherits the IEnumerable<T> interface so that if it represents a query, the results of that query can be
enumerated. Enumeration forces the expression tree associated with an IQueryable<T> object to be executed. Queries
that do not return enumerable results are executed when the Execute<TResult>(Expression) method is called.
The definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating
the expression tree to a query language appropriate for an underlying data source.
The IQueryable<T> interface enables queries to be polymorphic. That is, because a query against an IQueryable data
source is represented as an expression tree, it can be executed against different types of data sources.
The static ( Shared in Visual Basic) methods defined in the class Queryable (except for AsQueryable, ThenBy, and
ThenByDescending) extend objects of types that implement the IQueryable<T> interface.
For more information about how to create your own LINQ provider, see LINQ: Building an IQueryable Provider on
MSDN Blogs.
IQueryProvider Interface
Defines methods to create and execute queries that are described by an IQueryable object.
D eclaration
public interface IQueryProvider
I nheritance H ierarchy
None
Remarks
The IQueryProvider interface is intended for implementation by query providers.
For more information about how to create your own LINQ provider, see LINQ: Building an IQueryable Provider on
MSDN Blogs.
Methods
CreateQuery(Expression)
Constructs an IQueryable object that can evaluate the query represented by a specified expression tree.
D eclaration
public System.Linq.IQueryable CreateQuery (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
IQueryable
An IQueryable that can evaluate the query represented by the specified expression tree.
CreateQuery<TElement>(Expression)
Constructs an IQueryable<T> object that can evaluate the query represented by a specified expression tree.
D eclaration
public System.Linq.IQueryable<TElement> CreateQuery<TElement>
(System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
IQueryable<TElement>
An IQueryable<T> that can evaluate the query represented by the specified expression tree.
Execute(Expression)
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
Object
The value that results from executing the specified query.
Execute<TResult>(Expression)
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
TResult
The value that results from executing the specified query.
IQueryProvider.CreateQuery
I n this Article
Overloads
CreateQuery(Expression)
Constructs an IQueryable object that can evaluate the query
represented by a specified expression tree.
CreateQuery<TElement>(Expression)
Constructs an IQueryable<T> object that can evaluate the
query represented by a specified expression tree.
CreateQuery(Expression)
Constructs an IQueryable object that can evaluate the query represented by a specified expression tree.
public System.Linq.IQueryable CreateQuery (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
IQueryable
An IQueryable that can evaluate the query represented by the specified expression tree.
Remarks
Note
CreateQuery<TElement>(Expression)
Constructs an IQueryable<T> object that can evaluate the query represented by a specified expression tree.
public System.Linq.IQueryable<TElement> CreateQuery<TElement> (System.Linq.Expressions.Expression
expression);
Type Parameters
TElement
The type of the elements of the IQueryable<T> that is returned.
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
IQueryable<TElement>
An IQueryable<T> that can evaluate the query represented by the specified expression tree.
Remarks
Note
Overloads
Execute(Expression)
Executes the query represented by a specified expression tree.
Execute<TResult>(Expression)
Executes the strongly-typed query represented by a specified
expression tree.
Execute(Expression)
Executes the query represented by a specified expression tree.
public object Execute (System.Linq.Expressions.Expression expression);
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
Object
The value that results from executing the specified query.
Remarks
The Execute method executes queries that return a single value (instead of an enumerable sequence of values).
Expression trees that represent queries that return enumerable results are executed when their associated IQueryable
object is enumerated.
Execute<TResult>(Expression)
Executes the strongly-typed query represented by a specified expression tree.
public TResult Execute<TResult> (System.Linq.Expressions.Expression expression);
Type Parameters
TResult
The type of the value that results from executing the query.
Parameters
expression Expression
An expression tree that represents a LINQ query.
Returns
TResult
The value that results from executing the specified query.
Remarks
The Execute method executes queries that return a single value (instead of an enumerable sequence of values).
Expression trees that represent queries that return enumerable results are executed when the IQueryable<T> object
that contains the expression tree is enumerated.
The Queryable standard query operator methods that return singleton results call Execute. They pass it a
MethodCallExpression that represents a LINQ query.
Lookup<TKey,TElement> Class
Represents a collection of keys each mapped to one or more values.
D eclaration
public class Lookup<TKey,TElement> :
System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>>,
System.Linq.ILookup<TKey,TElement>
I nheritance H ierarchy
Object
Remarks
A Lookup<TKey,TElement> resembles a Dictionary<TKey,TValue>. The difference is that a Dictionary<TKey,TValue>
maps keys to single values, whereas a Lookup<TKey,TElement> maps keys to collections of values.
You can create an instance of a Lookup<TKey,TElement> by calling ToLookup on an object that implements
IEnumerable<T>.
Note
Properties
Count
P roperty Value
Int32
The number of key/value collection pairs in the Lookup<TKey,TElement>.
Item(TKey)
Parameters
key TKey
The key of the desired collection of values.
P roperty Value
System.Collections.Generic.IEnumerable<TElement>
The collection of values indexed by the specified key.
Methods
ApplyResultSelector<TResult>(Func<TKey,IEnumerable<TElement>,TResult>)
Applies a transform function to each key and its associated values and returns the results.
D eclaration
public System.Collections.Generic.IEnumerable<TResult> ApplyResultSelector<TResult>
(Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
Parameters
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
A function to project a result value from each key and its associated values.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection that contains one value for each key/value collection pair in the Lookup<TKey,TElement>.
Contains(TKey)
Parameters
key TKey
The key to find in the Lookup<TKey,TElement>.
Returns
Boolean
true if key is in the Lookup<TKey,TElement>; otherwise, false .
GetEnumerator()
Returns
System.Collections.Generic.IEnumerator<System.Linq.IGrouping<TKey,TElement>>
An enumerator for the Lookup<TKey,TElement>.
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through the Lookup<TKey,TElement>. This class cannot be inherited.
D eclaration
System.Collections.IEnumerator IEnumerable.GetEnumerator ();
Returns
IEnumerator
An enumerator for the Lookup<TKey,TElement>.
Lookup<TKey,TElement>.ApplyResultSelector
I n this Article
Applies a transform function to each key and its associated values and returns the results.
public System.Collections.Generic.IEnumerable<TResult> ApplyResultSelector<TResult>
(Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
Type Parameters
TResult
The type of the result values produced by resultSelector .
Parameters
resultSelector System.Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
A function to project a result value from each key and its associated values.
Returns
System.Collections.Generic.IEnumerable<TResult>
A collection that contains one value for each key/value collection pair in the Lookup<TKey,TElement>.
Lookup<TKey,TElement>.Contains
I n this Article
Determines whether a specified key is in the Lookup<TKey,TElement>.
public bool Contains (TKey key);
Parameters
key TKey
The key to find in the Lookup<TKey,TElement>.
Returns
Boolean
true if key is in the Lookup<TKey,TElement>; otherwise, false .
Examples
The following example demonstrates how to use Contains to determine whether a Lookup<TKey,TElement> contains a
specified key. This code example is part of a larger example provided for the Lookup<TKey,TElement> class.
// Determine if there is a key with the value 'G' in the Lookup.
bool hasG = lookup.Contains('G');
Lookup<TKey,TElement>.Count
I n this Article
Gets the number of key/value collection pairs in the Lookup<TKey,TElement>.
public int Count { get; }
Returns
Int32
The number of key/value collection pairs in the Lookup<TKey,TElement>.
Examples
The following example demonstrates how to use Count to determine the number of key/value collection pairs in a
Lookup<TKey,TElement>. This code example is part of a larger example provided for the Lookup<TKey,TElement>
class.
// Get the number of key-collection pairs in the Lookup.
int count = lookup.Count;
Remarks
The value of the Count property does not change because items cannot be added to or removed from a
Lookup<TKey,TElement> object after it has been created.
Lookup<TKey,TElement>.GetEnumerator
I n this Article
Returns a generic enumerator that iterates through the Lookup<TKey,TElement>.
public System.Collections.Generic.IEnumerator<System.Linq.IGrouping<TKey,TElement>> GetEnumerator
();
Returns
System.Collections.Generic.IEnumerator<System.Linq.IGrouping<TKey,TElement>>
An enumerator for the Lookup<TKey,TElement>.
Examples
The following example demonstrates how to use GetEnumerator to iterate through the keys and values of a
Lookup<TKey,TElement>. This code example is part of a larger example provided for the Lookup<TKey,TElement>
class.
// Iterate through each IGrouping in the Lookup and output the contents.
foreach (IGrouping<char, string> packageGroup in lookup)
{
// Print the key value of the IGrouping.
Console.WriteLine(packageGroup.Key);
// Iterate through each value in the IGrouping and print its value.
foreach (string str in packageGroup)
Console.WriteLine(" {0}", str);
}
Lookup<TKey,TElement>.Item
I n this Article
Gets the collection of values indexed by the specified key.
public System.Collections.Generic.IEnumerable<TElement> this[TKey key] { get; }
Parameters
key TKey
The key of the desired collection of values.
Returns
System.Collections.Generic.IEnumerable<TElement>
The collection of values indexed by the specified key.
Examples
The following example demonstrates how to use Item to index directly into a Lookup<TKey,TElement>. This code
example is part of a larger example provided for the Lookup<TKey,TElement> class.
// Select a collection of Packages by indexing directly into the Lookup.
IEnumerable<string> cgroup = lookup['C'];
Remarks
This indexed property provides the ability to index a specific collection of values in the Lookup<TKey,TElement> by
using the following syntax: myLookup[key] in Visual C# or myLookup(key) in Visual Basic. If the key is not found in
the collection, an empty sequence is returned.
OrderedParallelQuery<TSource> Class
Represents a sorted, parallel sequence.
D eclaration
public class OrderedParallelQuery<TSource> : System.Linq.ParallelQuery<TSource>
I nheritance H ierarchy
Object
ParallelQuery
ParallelQuery<TSource>
Methods
GetEnumerator()
Returns
System.Collections.Generic.IEnumerator<TSource>
An enumerator that iterates through the sequence.
OrderedParallelQuery<TSource>.GetEnumerator
I n this Article
Returns an enumerator that iterates through the sequence.
public override System.Collections.Generic.IEnumerator<TSource> GetEnumerator ();
Returns
System.Collections.Generic.IEnumerator<TSource>
An enumerator that iterates through the sequence.
ParallelEnumerable Class
Provides a set of methods for querying objects that implement ParallelQuery{TSource}. This is the parallel equivalent of
Enumerable.
D eclaration
public static class ParallelEnumerable
I nheritance H ierarchy
Object
Methods
Aggregate<TSource,TAccumulate,TResult>(ParallelQuery<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TResult>)
Applies in parallel an accumulator function over a sequence. The specified seed value is used as the initial
accumulator value, and the specified function is used to select the result value.
D eclaration
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Linq.ParallelQuery<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> func, Func<TAccumulate,TResult> resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
resultSelector Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Aggregate<TSource,TAccumulate,TResult>(ParallelQuery<TSource>, Func<TAccumulate>,
Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TAccumulate,TAccumulate>,
Func<TAccumulate,TResult>)
Applies in parallel an accumulator function over a sequence. This overload is not available in the sequential
implementation.
D eclaration
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TAccumulate> seedFactory,
Func<TAccumulate,TSource,TAccumulate> updateAccumulatorFunc,
Func<TAccumulate,TAccumulate,TAccumulate> combineAccumulatorsFunc, Func<TAccumulate,TResult>
resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seedFactory Func<TAccumulate>
A function that returns the initial accumulator value.
updateAccumulatorFunc Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element in a partition.
combineAccumulatorsFunc Func<TAccumulate,TAccumulate,TAccumulate>
An accumulator function to be invoked on the yielded accumulator result from each partition.
resultSelector Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Aggregate<TSource,TAccumulate,TResult>(ParallelQuery<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>, Func<TAccumulate,TAccumulate,TAccumulate>,
Func<TAccumulate,TResult>)
Applies in parallel an accumulator function over a sequence. This overload is not available in the sequential
implementation.
D eclaration
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Linq.ParallelQuery<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> updateAccumulatorFunc,
Func<TAccumulate,TAccumulate,TAccumulate> combineAccumulatorsFunc, Func<TAccumulate,TResult>
resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
updateAccumulatorFunc Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element in a partition.
combineAccumulatorsFunc Func<TAccumulate,TAccumulate,TAccumulate>
An accumulator function to be invoked on the yielded accumulator result from each partition.
resultSelector Func<TAccumulate,TResult>
resultSelector Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Aggregate<TSource,TAccumulate>(ParallelQuery<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>)
Applies in parallel an accumulator function over a sequence. The specified seed value is used as the initial
accumulator value.
D eclaration
public static TAccumulate Aggregate<TSource,TAccumulate> (this
System.Linq.ParallelQuery<TSource> source, TAccumulate seed,
Func<TAccumulate,TSource,TAccumulate> func);
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
Returns
TAccumulate
The final accumulator value.
Aggregate<TSource>(ParallelQuery<TSource>, Func<TSource,TSource,TSource>)
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
func Func<TSource,TSource,TSource>
An accumulator function to be invoked on each element.
Returns
TSource
The final accumulator value.
All<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Parameters
source ParallelQuery<TSource>
A sequence whose elements to apply the predicate to.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false..
Any<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
The sequence to check for emptiness.
Returns
Boolean
true if the source sequence contains any elements; otherwise, false.
Any<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Parameters
source ParallelQuery<TSource>
A sequence to whose elements the predicate will be applied.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Boolean
true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.
AsEnumerable<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
The sequence to cast as IEnumerable<T>.
Returns
IEnumerable<TSource>
The input sequence typed as IEnumerable<T>.
AsOrdered(ParallelQuery)
Enables treatment of a data source as if it were ordered, overriding the default of unordered. AsOrdered may only
be invoked on non-generic sequences returned by AsParallel, ParallelEnumerable.Range, and
ParallelEnumerable.Repeat.
D eclaration
public static System.Linq.ParallelQuery AsOrdered (this System.Linq.ParallelQuery source);
Parameters
source ParallelQuery
The input sequence.
Returns
ParallelQuery
The source sequence which will maintain the original ordering in the subsequent query operators.
AsOrdered<TSource>(ParallelQuery<TSource>)
Enables treatment of a data source as if it were ordered, overriding the default of unordered. AsOrdered may only
be invoked on generic sequences returned by AsParallel, ParallelEnumerable.Range, and
ParallelEnumerable.Repeat.
D eclaration
public static System.Linq.ParallelQuery<TSource> AsOrdered<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The input sequence.
Returns
ParallelQuery<TSource>
The source sequence which will maintain the original ordering in the subsequent query operators.
AsParallel(IEnumerable)
Parameters
source IEnumerable
An IEnumerable<T> to convert to a ParallelQuery.
Returns
ParallelQuery
The source as a ParallelQuery to bind to ParallelEnumerable extension methods.
AsParallel<TSource>(Partitioner<TSource>)
Enables parallelization of a query, as sourced by a custom partitioner that is responsible for splitting the input
sequence into partitions.
D eclaration
public static System.Linq.ParallelQuery<TSource> AsParallel<TSource> (this
System.Collections.Concurrent.Partitioner<TSource> source);
Parameters
source Partitioner<TSource>
A partitioner over the input sequence.
Returns
ParallelQuery<TSource>
The source as a ParallelQuery to bind to ParallelEnumerable extension methods.
AsParallel<TSource>(IEnumerable<TSource>)
Parameters
source IEnumerable<TSource>
An IEnumerable<T> to convert to a ParallelQuery<TSource>.
Returns
ParallelQuery<TSource>
The source as a ParallelQuery<TSource> to bind to ParallelEnumerable extension methods.
AsSequential<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
A ParallelQuery<TSource> to convert to an IEnumerable<T>.
Returns
IEnumerable<TSource>
The source as an IEnumerable<T> to bind to sequential extension methods.
AsUnordered<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
The input sequence.
Returns
ParallelQuery<TSource>
The source sequence with arbitrary order.
Average(ParallelQuery<Single>)
Parameters
source ParallelQuery<Single>
A sequence of values that are used to calculate an average.
Returns
Single
The average of the sequence of values.
Average(ParallelQuery<Nullable<Int64>>)
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Double>
The average of the sequence of values.
Average(ParallelQuery<Nullable<Int32>>)
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Double>
The average of the sequence of values.
Average(ParallelQuery<Nullable<Double>>)
Parameters
source ParallelQuery<Nullable<Double>>
The source sequence.
Returns
Nullable<Double>
Returns the average of the sequence of values.
Average(ParallelQuery<Nullable<Single>>)
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Single>
The average of the sequence of values.
Average(ParallelQuery<Int64>)
Parameters
source ParallelQuery<Int64>
A sequence of values that are used to calculate an average.
Returns
Double
The average of the sequence of values.
Average(ParallelQuery<Int32>)
Parameters
source ParallelQuery<Int32>
A sequence of values that are used to calculate an average.
Returns
Double
The average of the sequence of values.
Average(ParallelQuery<Double>)
Parameters
source ParallelQuery<Double>
A sequence of values that are used to calculate an average.
Returns
Double
The average of the sequence of values.
Average(ParallelQuery<Decimal>)
Parameters
source ParallelQuery<Decimal>
A sequence of values that are used to calculate an average.
Returns
Decimal
The average of the sequence of values.
Average(ParallelQuery<Nullable<Decimal>>)
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Decimal>
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static float Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,float> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int64>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<long>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int32>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<int>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Double>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<double>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Single>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<float> Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,long> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,int> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,double> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Decimal>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static decimal Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,decimal> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The average of the sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Decimal>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<decimal> Average<TSource> (this System.Linq.ParallelQuery<TSource>
source, Func<TSource,Nullable<decimal>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The average of the sequence of values.
Cast<TResult>(ParallelQuery)
Parameters
source ParallelQuery
The sequence that contains the elements to be converted.
Returns
ParallelQuery<TResult>
A sequence that contains each element of the source sequence converted to the specified type.
Concat<TSource>(ParallelQuery<TSource>, IEnumerable<TSource>)
This Concat overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Concat<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Concat<TSource>(ParallelQuery<TSource>, ParallelQuery<TSource>)
Parameters
first ParallelQuery<TSource>
The first sequence to concatenate.
second ParallelQuery<TSource>
The sequence to concatenate to the first sequence.
Returns
ParallelQuery<TSource>
A sequence that contains the concatenated elements of the two input sequences.
Parameters
source ParallelQuery<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
comparer IEqualityComparer<TSource>
An equality comparer to compare values.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false.
Contains<TSource>(ParallelQuery<TSource>, TSource)
Determines in parallel whether a sequence contains a specified element by using the default equality comparer.
D eclaration
public static bool Contains<TSource> (this System.Linq.ParallelQuery<TSource> source, TSource
value);
Parameters
source ParallelQuery<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false.
Count<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
Returns
Int32
The number of elements in the input sequence.
Count<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns a number that represents how many elements in the specified parallel sequence satisfy a condition.
D eclaration
public static int Count<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Int32
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
DefaultIfEmpty<TSource>(ParallelQuery<TSource>)
Returns the elements of the specified parallel sequence or the type parameter's default value in a singleton
collection if the sequence is empty.
D eclaration
public static System.Linq.ParallelQuery<TSource> DefaultIfEmpty<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The sequence to return a default value for if it is empty.
Returns
ParallelQuery<TSource>
A sequence that contains default(TSource) if source is empty; otherwise, source .
DefaultIfEmpty<TSource>(ParallelQuery<TSource>, TSource)
Returns the elements of the specified parallel sequence or the specified value in a singleton collection if the
sequence is empty.
D eclaration
public static System.Linq.ParallelQuery<TSource> DefaultIfEmpty<TSource> (this
System.Linq.ParallelQuery<TSource> source, TSource defaultValue);
Parameters
source ParallelQuery<TSource>
The sequence to return the specified value for if it is empty.
defaultValue TSource
The value to return if the sequence is empty.
Returns
ParallelQuery<TSource>
A sequence that contains defaultValue if source is empty; otherwise, source .
Distinct<TSource>(ParallelQuery<TSource>)
Returns distinct elements from a parallel sequence by using the default equality comparer to compare values.
D eclaration
public static System.Linq.ParallelQuery<TSource> Distinct<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The sequence to remove duplicate elements from.
Returns
ParallelQuery<TSource>
A sequence that contains distinct elements from the source sequence.
Distinct<TSource>(ParallelQuery<TSource>, IEqualityComparer<TSource>)
Returns distinct elements from a parallel sequence by using a specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Linq.ParallelQuery<TSource> Distinct<TSource> (this
System.Linq.ParallelQuery<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>
comparer);
Parameters
source ParallelQuery<TSource>
The sequence to remove duplicate elements from.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains distinct elements from the source sequence.
ElementAt<TSource>(ParallelQuery<TSource>, Int32)
Parameters
source ParallelQuery<TSource>
A sequence to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
The element at the specified position in the source sequence.
ElementAtOrDefault<TSource>(ParallelQuery<TSource>, Int32)
Returns the element at a specified index in a parallel sequence or a default value if the index is out of range.
D eclaration
public static TSource ElementAtOrDefault<TSource> (this System.Linq.ParallelQuery<TSource>
source, int index);
Parameters
source ParallelQuery<TSource>
A sequence to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
default(TSource) if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source
sequence.
Empty<TResult>()
Returns
ParallelQuery<TResult>
An empty sequence whose type argument is TResult .
Except<TSource>(ParallelQuery<TSource>, IEnumerable<TSource>)
This Except overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Except<TSource>(ParallelQuery<TSource>, ParallelQuery<TSource>)
Produces the set difference of two parallel sequences by using the default equality comparer to compare values.
D eclaration
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Parameters
first ParallelQuery<TSource>
A sequence whose elements that are not also in second will be returned.
second ParallelQuery<TSource>
A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.
Returns
ParallelQuery<TSource>
A sequence that contains the set difference of the elements of two sequences.
This Except overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second, System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Produces the set difference of two parallel sequences by using the specified IEqualityComparer<T> to compare
values.
D eclaration
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
A sequence whose elements that are not also in second will be returned.
second ParallelQuery<TSource>
A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.
comparer IEqualityComparer<TSource>
IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains the set difference of the elements of two sequences.
First<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
The sequence to return the first element of.
Returns
TSource
The first element in the specified sequence.
First<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the first element in a parallel sequence that satisfies a specified condition.
D eclaration
public static TSource First<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
The first element in the sequence that passes the test in the specified predicate function.
FirstOrDefault<TSource>(ParallelQuery<TSource>)
Returns the first element of a parallel sequence, or a default value if the sequence contains no elements.
D eclaration
public static TSource FirstOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The sequence to return the first element of.
Returns
TSource
default(TSource) if source is empty; otherwise, the first element in source .
FirstOrDefault<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the first element of the parallel sequence that satisfies a condition or a default value if no such element is
found.
D eclaration
public static TSource FirstOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
default(TSource) if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that
passes the test specified by predicate.
ForAll<TSource>(ParallelQuery<TSource>, Action<TSource>)
Invokes in parallel the specified action for each element in the source .
D eclaration
public static void ForAll<TSource> (this System.Linq.ParallelQuery<TSource> source,
Action<TSource> action);
Parameters
source ParallelQuery<TSource>
The ParallelQuery<TSource> whose elements will be processed by action .
action Action<TSource>
An Action to invoke on each element.
GroupBy<TSource,TKey,TElement,TResult>(ParallelQuery<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>, IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. Key values are compared by using a specified comparer, and the elements of each group
are projected by using a specified function.
D eclaration
public static System.Linq.ParallelQuery<TResult> GroupBy<TSource,TKey,TElement,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
Func<TSource,TElement> elementSelector,
Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping{Key, TElement}.
resultSelector Func<TKey,IEnumerable<TElement>,TResult>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ParallelQuery<TResult>
A sequence of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey,TElement,TResult>(ParallelQuery<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
Groups in parallel the elements of a sequence according to a specified key selector function and creates a result
value from each group and its key. The elements of each group are projected by using a specified function.
D eclaration
public static System.Linq.ParallelQuery<TResult> GroupBy<TSource,TKey,TElement,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
Func<TSource,TElement> elementSelector,
Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping.
resultSelector Func<TKey,IEnumerable<TElement>,TResult>
A function to create a result value from each group.
Returns
ParallelQuery<TResult>
A sequence of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey,TElement>(ParallelQuery<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
Groups in parallel the elements of a sequence according to a specified key selector function and projects the
elements for each group by using a specified function.
D eclaration
public static System.Linq.ParallelQuery<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery<TSource> that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
Returns
ParallelQuery<IGrouping<TKey,TElement>>
A sequence of groups that are sorted descending according to TKey .
Groups in parallel the elements of a sequence according to a key selector function. The keys are compared by
using a comparer and each group's elements are projected by using a specified function.
D eclaration
public static System.Linq.ParallelQuery<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery{TSource}that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping.
comparer IEqualityComparer<TKey>
An IComparer{TSource} to compare keys.
Returns
ParallelQuery<IGrouping<TKey,TElement>>
A sequence of groups that are sorted descending according to TKey .
GroupBy<TSource,TKey,TResult>(ParallelQuery<TSource>, Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>)
Groups in parallel the elements of a sequence according to a specified key selector function and creates a result
value from each group and its key.
D eclaration
public static System.Linq.ParallelQuery<TResult> GroupBy<TSource,TKey,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
resultSelector Func<TKey,IEnumerable<TSource>,TResult>
A function to create a result value from each group.
Returns
ParallelQuery<TResult>
A sequence of elements of type TResult where each element represents a projection over a group and its key.
GroupBy<TSource,TKey,TResult>(ParallelQuery<TSource>, Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)
Groups in parallel the elements of a sequence according to a specified key selector function and creates a result
value from each group and its key. The keys are compared by using a specified comparer.
D eclaration
public static System.Linq.ParallelQuery<TResult> GroupBy<TSource,TKey,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
resultSelector Func<TKey,IEnumerable<TSource>,TResult>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ParallelQuery<TResult>
A sequence of groups.
Groups in parallel the elements of a sequence according to a specified key selector function and compares the keys
by using a specified IComparer<T>.
D eclaration
public static System.Linq.ParallelQuery<System.Linq.IGrouping<TKey,TSource>>
GroupBy<TSource,TKey> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey>
keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery<TSource> that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IEqualityComparer<TKey>
An IComparer<T> to compare keys.
Returns
ParallelQuery<IGrouping<TKey,TSource>>
A sequence of groups that are sorted descending according to TKey .
GroupBy<TSource,TKey>(ParallelQuery<TSource>, Func<TSource,TKey>)
Groups in parallel the elements of a sequence according to a specified key selector function.
D eclaration
public static System.Linq.ParallelQuery<System.Linq.IGrouping<TKey,TSource>>
GroupBy<TSource,TKey> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey>
keySelector);
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery{TSource}that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
ParallelQuery<IGrouping<TKey,TSource>>
A sequence of groups that are sorted descending according to TKey .
This GroupJoin overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector);
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Correlates in parallel the elements of two sequences based on equality of keys and groups the results. The default
equality comparer is used to compare keys.
D eclaration
public static System.Linq.ParallelQuery<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Linq.ParallelQuery<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector);
Parameters
outer ParallelQuery<TOuter>
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing a grouped join on two sequences.
This GroupJoin overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
This parameter is not used.
comparer IEqualityComparer<TKey>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Correlates in parallel the elements of two sequences based on key equality and groups the results. A specified
IEqualityComparer<T> is used to compare keys.
D eclaration
public static System.Linq.ParallelQuery<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Linq.ParallelQuery<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing a grouped join on two sequences.
This Intersect overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Intersect<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second, System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Produces the set intersection of two parallel sequences by using the specified IEqualityComparer{T} to compare
values.
D eclaration
public static System.Linq.ParallelQuery<TSource> Intersect<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements that also appear in second will be returned.
second ParallelQuery<TSource>
A sequence whose distinct elements that also appear in the first sequence will be returned.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Intersect<TSource>(ParallelQuery<TSource>, IEnumerable<TSource>)
This Intersect overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Intersect<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Intersect<TSource>(ParallelQuery<TSource>, ParallelQuery<TSource>)
Produces the set intersection of two parallel sequences by using the default equality comparer to compare values.
D eclaration
public static System.Linq.ParallelQuery<TSource> Intersect<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements that also appear in second will be returned.
second ParallelQuery<TSource>
A sequence whose distinct elements that also appear in the first sequence will be returned.
Returns
ParallelQuery<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
This Join overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when invoked.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector);
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,TInner,TResult>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Correlates in parallel the elements of two sequences based on matching keys. The default equality comparer is
used to compare keys.
D eclaration
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Linq.ParallelQuery<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector);
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
This Join overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when invoked.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>
comparer);
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,TInner,TResult>
This parameter is not used.
comparer IEqualityComparer<TKey>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>, ParallelQuery<TInner>, Func<TOuter,TKey>,
Func<TInner,TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)
Correlates in parallel the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is
used to compare keys.
D eclaration
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Linq.ParallelQuery<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,TInner,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>
comparer);
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
Last<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
The sequence to return the last element from.
Returns
TSource
The value at the last position in the source sequence.
Last<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the last element of a parallel sequence that satisfies a specified condition.
D eclaration
public static TSource Last<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
The last element in the sequence that passes the test in the specified predicate function.
LastOrDefault<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the last element of a parallel sequence that satisfies a condition, or a default value if no such element is
found.
D eclaration
public static TSource LastOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
default() if the sequence is empty or if no elements pass the test in the predicate function; otherwise, the last element that passes the test in
the predicate function.
LastOrDefault<TSource>(ParallelQuery<TSource>)
Returns the last element of a parallel sequence, or a default value if the sequence contains no elements.
D eclaration
public static TSource LastOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
Returns
TSource
default() if the source sequence is empty; otherwise, the last element in the sequence.
LongCount<TSource>(ParallelQuery<TSource>)
Returns an Int64 that represents the total number of elements in a parallel sequence.
D eclaration
public static long LongCount<TSource> (this System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
Returns
Int64
The number of elements in the input sequence.
LongCount<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns an Int64 that represents how many elements in a parallel sequence satisfy a condition.
D eclaration
public static long LongCount<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Int64
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
Max(ParallelQuery<Nullable<Single>>)
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Single>
The maximum value in the sequence.
Max(ParallelQuery<Single>)
Parameters
source ParallelQuery<Single>
A sequence of values to determine the maximum value of.
Returns
Single
The maximum value in the sequence.
Max(ParallelQuery<Nullable<Int64>>)
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Int64>
The maximum value in the sequence.
Max(ParallelQuery<Nullable<Int32>>)
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Int32>
The maximum value in the sequence.
Max(ParallelQuery<Nullable<Double>>)
Parameters
source ParallelQuery<Nullable<Double>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Double>
The maximum value in the sequence.
Max(ParallelQuery<Double>)
Parameters
source ParallelQuery<Double>
A sequence of values to determine the maximum value of.
Returns
Double
The maximum value in the sequence.
Max(ParallelQuery<Int64>)
Parameters
source ParallelQuery<Int64>
A sequence of values to determine the maximum value of.
Returns
Int64
The maximum value in the sequence.
Max(ParallelQuery<Int32>)
Parameters
source ParallelQuery<Int32>
A sequence of values to determine the maximum value of.
Returns
Int32
The maximum value in the sequence.
Max(ParallelQuery<Decimal>)
Parameters
source ParallelQuery<Decimal>
A sequence of values to determine the maximum value of.
Returns
Decimal
The maximum value in the sequence.
Max(ParallelQuery<Nullable<Decimal>>)
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Decimal>
The maximum value in the sequence.
Max<TSource,TResult>(ParallelQuery<TSource>, Func<TSource,TResult>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static TResult Max<TSource,TResult> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,TResult> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int64>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static Nullable<long> Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<long>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Int64>
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static float Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,float> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Single>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static Nullable<float> Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int32>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static Nullable<int> Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<int>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Int32>
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Double>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static Nullable<double> Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<double>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Decimal>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static Nullable<decimal> Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<decimal>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static long Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,long> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Int64
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static int Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,int> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Int32
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static double Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,double> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Decimal>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
D eclaration
public static decimal Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,decimal> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The maximum value in the sequence.
Max<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
Returns
TSource
The maximum value in the sequence.
Min(ParallelQuery<Single>)
Parameters
source ParallelQuery<Single>
A sequence of values to determine the minimum value of.
Returns
Single
The minimum value in the sequence.
Min(ParallelQuery<Nullable<Single>>)
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Single>
The minimum value in the sequence.
Min(ParallelQuery<Nullable<Int64>>)
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Int64>
The minimum value in the sequence.
Min(ParallelQuery<Nullable<Int32>>)
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Int32>
The minimum value in the sequence.
Min(ParallelQuery<Nullable<Double>>)
Parameters
source ParallelQuery<Nullable<Double>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Double>
The minimum value in the sequence.
Min(ParallelQuery<Nullable<Decimal>>)
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Decimal>
The minimum value in the sequence.
Min(ParallelQuery<Int64>)
Parameters
source ParallelQuery<Int64>
A sequence of values to determine the minimum value of.
Returns
Int64
The minimum value in the sequence.
Min(ParallelQuery<Int32>)
Parameters
source ParallelQuery<Int32>
A sequence of values to determine the minimum value of.
Returns
Int32
The minimum value in the sequence.
Min(ParallelQuery<Double>)
Parameters
source ParallelQuery<Double>
A sequence of values to determine the minimum value of.
Returns
Double
The minimum value in the sequence.
Min(ParallelQuery<Decimal>)
Parameters
source ParallelQuery<Decimal>
A sequence of values to determine the minimum value of.
Returns
Decimal
The minimum value in the sequence.
Min<TSource,TResult>(ParallelQuery<TSource>, Func<TSource,TResult>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static TResult Min<TSource,TResult> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,TResult> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static float Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,float> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Single>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static Nullable<float> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int32>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static Nullable<int> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<int>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Int32>
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Double>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static Nullable<double> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<double>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int64>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static Nullable<long> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<long>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Int64>
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static long Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,long> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Int64
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static int Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,int> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Int32
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Decimal>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static Nullable<decimal> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<decimal>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static double Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,double> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Decimal>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
D eclaration
public static decimal Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,decimal> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The minimum value in the sequence.
Min<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
Returns
TSource
The minimum value in the sequence.
OfType<TResult>(ParallelQuery)
Parameters
source ParallelQuery
The sequence whose elements to filter.
Returns
ParallelQuery<TResult>
A sequence that contains elements from the input sequence of type .
OrderBy<TSource,TKey>(ParallelQuery<TSource>, Func<TSource,TKey>)
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
Sorts in parallel the elements of a sequence in ascending order by using a specified comparer.
D eclaration
public static System.Linq.OrderedParallelQuery<TSource> OrderBy<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
OrderByDescending<TSource,TKey>(ParallelQuery<TSource>, Func<TSource,TKey>)
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted descending according to a key.
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted descending according to a key.
Range(Int32, Int32)
Parameters
start Int32
The value of the first integer in the sequence.
count Int32
The number of sequential integers to generate.
Returns
ParallelQuery<Int32>
An IEnumerable in C# or IEnumerable(Of Int32) in Visual Basic that contains a range of sequential integral numbers.
Repeat<TResult>(TResult, Int32)
Parameters
element TResult
The value to be repeated.
count Int32
The number of times to repeat the value in the generated sequence.
Returns
ParallelQuery<TResult>
A sequence that contains a repeated value.
Reverse<TSource>(ParallelQuery<TSource>)
Returns
ParallelQuery<TSource>
A sequence whose elements correspond to those of the input sequence in reverse order.
Select<TSource,TResult>(ParallelQuery<TSource>, Func<TSource,Int32,TResult>)
Projects in parallel each element of a sequence into a new form by incorporating the element's index.
D eclaration
public static System.Linq.ParallelQuery<TResult> Select<TSource,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,TResult> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to invoke a transform function on.
selector Func<TSource,Int32,TResult>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the transform function on each element of source , based on the index supplied to
selector .
Select<TSource,TResult>(ParallelQuery<TSource>, Func<TSource,TResult>)
Parameters
source ParallelQuery<TSource>
A sequence of values to invoke a transform function on.
selector Func<TSource,TResult>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the transform function on each element of source .
SelectMany<TSource,TCollection,TResult>(ParallelQuery<TSource>, Func<TSource,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence,
and invokes a result selector function on each element therein.
D eclaration
public static System.Linq.ParallelQuery<TResult> SelectMany<TSource,TCollection,TResult> (this
System.Linq.ParallelQuery<TSource> source,
Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector,
Func<TSource,TCollection,TResult> resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
collectionSelector Func<TSource,IEnumerable<TCollection>>
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
resultSelector Func<TSource,TCollection,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of
source based on the index supplied to collectionSelector , and then mapping each of those sequence elements and their
corresponding source element to a result element.
SelectMany<TSource,TCollection,TResult>(ParallelQuery<TSource>,
Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence,
and invokes a result selector function on each element therein. The index of each source element is used in the
intermediate projected form of that element.
D eclaration
public static System.Linq.ParallelQuery<TResult> SelectMany<TSource,TCollection,TResult> (this
System.Linq.ParallelQuery<TSource> source,
Func<TSource,int,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector,
Func<TSource,TCollection,TResult> resultSelector);
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
collectionSelector Func<TSource,Int32,IEnumerable<TCollection>>
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
resultSelector Func<TSource,TCollection,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of
source based on the index supplied to collectionSelector , and then mapping each of those sequence elements and their
corresponding source element to a result element.
SelectMany<TSource,TResult>(ParallelQuery<TSource>, Func<TSource,IEnumerable<TResult>>)
Projects in parallel each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into
one sequence.
D eclaration
public static System.Linq.ParallelQuery<TResult> SelectMany<TSource,TResult> (this
System.Linq.ParallelQuery<TSource> source,
Func<TSource,System.Collections.Generic.IEnumerable<TResult>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
selector Func<TSource,IEnumerable<TResult>>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
SelectMany<TSource,TResult>(ParallelQuery<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
Projects in parallel each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into
one sequence. The index of each source element is used in the projected form of that element.
D eclaration
public static System.Linq.ParallelQuery<TResult> SelectMany<TSource,TResult> (this
System.Linq.ParallelQuery<TSource> source,
Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
selector Func<TSource,Int32,IEnumerable<TResult>>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
SequenceEqual<TSource>(ParallelQuery<TSource>, IEnumerable<TSource>)
This SequenceEqual overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
Boolean
This overload always throws a NotSupportedException.
SequenceEqual<TSource>(ParallelQuery<TSource>, ParallelQuery<TSource>)
Determines whether two parallel sequences are equal by comparing the elements by using the default equality
comparer for their type.
D eclaration
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Linq.ParallelQuery<TSource> second);
Parameters
first ParallelQuery<TSource>
A sequence to compare to second.
second ParallelQuery<TSource>
A sequence to compare to the first input sequence.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality
comparer for their type; otherwise, false.
This SequenceEqual overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
Boolean
This overload always throws a NotSupportedException.
Determines whether two parallel sequences are equal by comparing their elements by using a specified
IEqualityComparer{T}.
D eclaration
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Linq.ParallelQuery<TSource> second, System.Collections.Generic.IEqualityComparer<TSource>
comparer);
Parameters
first ParallelQuery<TSource>
A sequence to compare to second .
second ParallelQuery<TSource>
A sequence to compare to the first input sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to use to compare elements.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality
comparer for their type; otherwise, false.
Single<TSource>(ParallelQuery<TSource>)
Returns the only element of a parallel sequence, and throws an exception if there is not exactly one element in the
sequence.
D eclaration
public static TSource Single<TSource> (this System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
Returns
TSource
The single element of the input sequence.
Single<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the only element of a parallel sequence that satisfies a specified condition, and throws an exception if
more than one such element exists.
D eclaration
public static TSource Single<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
predicate Func<TSource,Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies a condition.
SingleOrDefault<TSource>(ParallelQuery<TSource>)
Returns the only element of a parallel sequence, or a default value if the sequence is empty; this method throws an
exception if there is more than one element in the sequence.
D eclaration
public static TSource SingleOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source);
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
Returns
TSource
TSource
The single element of the input sequence, or default() if the sequence contains no elements.
SingleOrDefault<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the only element of a parallel sequence that satisfies a specified condition or a default value if no such
element exists; this method throws an exception if more than one element satisfies the condition.
D eclaration
public static TSource SingleOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
predicate Func<TSource,Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition, or default() if no such element is found.
Skip<TSource>(ParallelQuery<TSource>, Int32)
Bypasses a specified number of elements in a parallel sequence and then returns the remaining elements.
D eclaration
public static System.Linq.ParallelQuery<TSource> Skip<TSource> (this
System.Linq.ParallelQuery<TSource> source, int count);
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
count Int32
The number of elements to skip before returning the remaining elements.
Returns
ParallelQuery<TSource>
A sequence that contains the elements that occur after the specified index in the input sequence.
SkipWhile<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Bypasses elements in a parallel sequence as long as a specified condition is true and then returns the remaining
elements.
D eclaration
D eclaration
public static System.Linq.ParallelQuery<TSource> SkipWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test
specified by predicate.
SkipWhile<TSource>(ParallelQuery<TSource>, Func<TSource,Int32,Boolean>)
Bypasses elements in a parallel sequence as long as a specified condition is true and then returns the remaining
elements. The element's index is used in the logic of the predicate function.
D eclaration
public static System.Linq.ParallelQuery<TSource> SkipWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Int32,Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test
specified by predicate.
Sum(ParallelQuery<Nullable<Single>>)
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values to calculate the sum of.
Returns
Nullable<Single>
The sum of the projected values in the sequence.
Sum(ParallelQuery<Nullable<Int64>>)
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values to calculate the sum of.
Returns
Nullable<Int64>
The sum of the projected values in the sequence.
Sum(ParallelQuery<Nullable<Int32>>)
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values to calculate the sum of.
Returns
Nullable<Int32>
The sum of the projected values in the sequence.
Sum(ParallelQuery<Nullable<Double>>)
Parameters
source ParallelQuery<Nullable<Double>>
A sequence of values to calculate the sum of.
Returns
Nullable<Double>
The sum of the projected values in the sequence.
Sum(ParallelQuery<Single>)
Parameters
source ParallelQuery<Single>
A sequence of values to calculate the sum of.
Returns
Single
The sum of the projected values in the sequence.
Sum(ParallelQuery<Int64>)
Parameters
source ParallelQuery<Int64>
A sequence of values to calculate the sum of.
Returns
Int64
The sum of the projected values in the sequence.
Sum(ParallelQuery<Int32>)
Parameters
source ParallelQuery<Int32>
A sequence of values to calculate the sum of.
Returns
Int32
The sum of the projected values in the sequence.
Sum(ParallelQuery<Double>)
Parameters
source ParallelQuery<Double>
A sequence of values to calculate the sum of.
Returns
Double
The sum of the projected values in the sequence.
Sum(ParallelQuery<Decimal>)
Parameters
source ParallelQuery<Decimal>
A sequence of values to calculate the sum of.
Returns
Decimal
The sum of the projected values in the sequence.
Sum(ParallelQuery<Nullable<Decimal>>)
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values to calculate the sum of.
Returns
Nullable<Decimal>
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static float Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,float> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Decimal>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static decimal Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,decimal> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static double Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,double> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static int Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,int> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Int32
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static long Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,long> selector);
Parameters
source ParallelQuery<TSource>
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Int64
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Decimal>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<decimal> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<decimal>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Double>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<double> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<double>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int32>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<int> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<int>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Int32>
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Int64>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
public static Nullable<long> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<long>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Int64>
The sum of the projected values in the sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Nullable<Single>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on
each element of the input sequence.
D eclaration
D eclaration
public static Nullable<float> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The sum of the projected values in the sequence.
Take<TSource>(ParallelQuery<TSource>, Int32)
Returns a specified number of contiguous elements from the start of a parallel sequence.
D eclaration
public static System.Linq.ParallelQuery<TSource> Take<TSource> (this
System.Linq.ParallelQuery<TSource> source, int count);
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
count Int32
The number of elements to return.
Returns
ParallelQuery<TSource>
A sequence that contains the specified number of elements from the start of the input sequence.
TakeWhile<TSource>(ParallelQuery<TSource>, Func<TSource,Int32,Boolean>)
Returns elements from a parallel sequence as long as a specified condition is true. The element's index is used in
the logic of the predicate function.
D eclaration
public static System.Linq.ParallelQuery<TSource> TakeWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,bool> predicate);
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Int32,Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Returns
ParallelQuery<TSource>
A sequence that contains elements from the input sequence that occur before the element at which the test no longer passes.
TakeWhile<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.
ThenBy<TSource,TKey>(OrderedParallelQuery<TSource>, Func<TSource,TKey>)
Performs in parallel a subsequent ordering of the elements in a sequence in ascending order according to a key.
D eclaration
public static System.Linq.OrderedParallelQuery<TSource> ThenBy<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
ThenByDescending<TSource,TKey>(OrderedParallelQuery<TSource>, Func<TSource,TKey>)
Performs in parallel a subsequent ordering of the elements in a sequence in descending order, according to a key.
D eclaration
public static System.Linq.OrderedParallelQuery<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
A sequence whose elements are sorted descending according to a key.
Performs in parallel a subsequent ordering of the elements in a sequence in descending order by using a specified
comparer.
D eclaration
public static System.Linq.OrderedParallelQuery<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
A sequence whose elements are sorted descending according to a key.
ToArray<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
A sequence to create an array from.
Returns
TSource[]
An array that contains the elements from the input sequence.
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence
ToDictionary<TSource,TKey>(ParallelQuery<TSource>, Func<TSource,TKey>)
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
Returns
Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
ToList<TSource>(ParallelQuery<TSource>)
Parameters
source ParallelQuery<TSource>
A sequence to create a List<T> from.
Returns
List<TSource>
A List<T> that contains elements from the input sequence.
ToLookup<TSource,TKey,TElement>(ParallelQuery<TSource>, Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ILookup<TKey,TElement>
A Lookup<(Of <(TKey, TElement>)>) that contains values of type TElement selected from the input sequence.
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
ILookup<TKey,TElement>
A ILookup<TKey,TElement> that contains values of type TElement selected from the input sequence.
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ILookup<TKey,TSource>
A ILookup<TKey,TElement> that contains keys and values.
ToLookup<TSource,TKey>(ParallelQuery<TSource>, Func<TSource,TKey>)
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
Returns
ILookup<TKey,TSource>
A ILookup<TKey,TElement> that contains keys and values.
Union<TSource>(ParallelQuery<TSource>, IEnumerable<TSource>)
This Union overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Union<TSource>(ParallelQuery<TSource>, ParallelQuery<TSource>)
Produces the set union of two parallel sequences by using the default equality comparer.
D eclaration
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements form the first set for the union.
second ParallelQuery<TSource>
A sequence whose distinct elements form the second set for the union.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from both input sequences, excluding duplicates.
This Union overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource>
second, System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Produces the set union of two parallel sequences by using a specified IEqualityComparer{T}.
D eclaration
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements form the first set for the union.
second ParallelQuery<TSource>
A sequence whose distinct elements form the second set for the union.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from both input sequences, excluding duplicates.
Where<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Parameters
source ParallelQuery<TSource>
A sequence to filter.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains elements from the input sequence that satisfy the condition.
Where<TSource>(ParallelQuery<TSource>, Func<TSource,Int32,Boolean>)
Filters in parallel a sequence of values based on a predicate. Each element's index is used in the logic of the
predicate function.
D eclaration
public static System.Linq.ParallelQuery<TSource> Where<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,bool> predicate);
Parameters
source ParallelQuery<TSource>
A sequence to filter.
predicate Func<TSource,Int32,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains elements from the input sequence that satisfy the condition.
WithCancellation<TSource>(ParallelQuery<TSource>, CancellationToken)
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the option.
cancellationToken CancellationToken
A cancellation token.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, but with the registered cancellation token.
WithDegreeOfParallelism<TSource>(ParallelQuery<TSource>, Int32)
Sets the degree of parallelism to use in a query. Degree of parallelism is the maximum number of concurrently
executing tasks that will be used to process the query.
D eclaration
public static System.Linq.ParallelQuery<TSource> WithDegreeOfParallelism<TSource> (this
System.Linq.ParallelQuery<TSource> source, int degreeOfParallelism);
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the limit on the degrees of parallelism.
degreeOfParallelism Int32
The degree of parallelism for the query. The default value is Math.Min(ProcessorCount, MAX_SUPPORTED_DOP ) where
MAX_SUPPORTED_DOP is 512.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, with the limit on the degrees of parallelism set.
WithExecutionMode<TSource>(ParallelQuery<TSource>, ParallelExecutionMode)
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the option.
executionMode ParallelExecutionMode
The mode in which to execute the query.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, but with the registered execution mode.
WithMergeOptions<TSource>(ParallelQuery<TSource>, ParallelMergeOptions)
Sets the merge options for this query, which specify how the query will buffer output.
D eclaration
public static System.Linq.ParallelQuery<TSource> WithMergeOptions<TSource> (this
System.Linq.ParallelQuery<TSource> source, System.Linq.ParallelMergeOptions mergeOptions);
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the option.
mergeOptions ParallelMergeOptions
The merge options to set for this query.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, but with the registered merge options.
This Zip overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when invoked.
D eclaration
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> Zip<TFirst,TSecond,TResult> (this
System.Linq.ParallelQuery<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second,
Func<TFirst,TSecond,TResult> resultSelector);
Parameters
first ParallelQuery<TFirst>
This parameter is not used.
second IEnumerable<TSecond>
This parameter is not used.
resultSelector Func<TFirst,TSecond,TResult>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Zip<TFirst,TSecond,TResult>(ParallelQuery<TFirst>, ParallelQuery<TSecond>,
Func<TFirst,TSecond,TResult>)
Merges in parallel two sequences by using the specified predicate function.
D eclaration
public static System.Linq.ParallelQuery<TResult> Zip<TFirst,TSecond,TResult> (this
System.Linq.ParallelQuery<TFirst> first, System.Linq.ParallelQuery<TSecond> second,
Func<TFirst,TSecond,TResult> resultSelector);
Parameters
first ParallelQuery<TFirst>
The first sequence to zip.
second ParallelQuery<TSecond>
The second sequence to zip.
resultSelector Func<TFirst,TSecond,TResult>
A function to create a result element from two matching elements.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing resultSelector pairwise on two sequences. If the
sequence lengths are unequal, this truncates to the length of the shorter sequence.
ParallelEnumerable.Aggregate
I n this Article
Overloads
Aggregate<TSource,TAccumulate,TResult>(ParallelQuery<
TSource>, TAccumulate, Func<TAccumulate,TSource, Applies in parallel an accumulator function over a sequence.
TAccumulate>, Func<TAccumulate,TResult>) The specified seed value is used as the initial accumulator
value, and the specified function is used to select the result
value.
Aggregate<TSource,TAccumulate,TResult>(ParallelQuery<
TSource>, Func<TAccumulate>, Func<TAccumulate,TSource, Applies in parallel an accumulator function over a sequence.
TAccumulate>, Func<TAccumulate,TAccumulate, This overload is not available in the sequential implementation.
TAccumulate>, Func<TAccumulate,TResult>)
Aggregate<TSource,TAccumulate,TResult>(ParallelQuery<
TSource>, TAccumulate, Func<TAccumulate,TSource, Applies in parallel an accumulator function over a sequence.
TAccumulate>, Func<TAccumulate,TAccumulate, This overload is not available in the sequential implementation.
TAccumulate>, Func<TAccumulate,TResult>)
Aggregate<TSource,TAccumulate>(ParallelQuery<TSource>,
TAccumulate, Func<TAccumulate,TSource,TAccumulate>) Applies in parallel an accumulator function over a sequence.
The specified seed value is used as the initial accumulator
value.
Aggregate<TSource>(ParallelQuery<TSource>, Func<TSource,
TSource,TSource>) Applies in parallel an accumulator function over a sequence.
Aggregate<TSource,TAccumulate,TResult>
(ParallelQuery<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>,
Func<TAccumulate,TResult>)
Applies in parallel an accumulator function over a sequence. The specified seed value is used as the initial accumulator
value, and the specified function is used to select the result value.
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Linq.ParallelQuery<TSource> source, TAccumulate seed, Func<TAccumulate,TSource,TAccumulate>
func, Func<TAccumulate,TResult> resultSelector);
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
TResult
The type of the resulting value.
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
resultSelector Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or func or resultSelector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Aggregate<TSource,TAccumulate,TResult>
(ParallelQuery<TSource>, Func<TAccumulate>,
Func<TAccumulate,TSource,TAccumulate>,
Func<TAccumulate,TAccumulate,TAccumulate>,
Func<TAccumulate,TResult>)
Applies in parallel an accumulator function over a sequence. This overload is not available in the sequential
implementation.
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TAccumulate> seedFactory,
Func<TAccumulate,TSource,TAccumulate> updateAccumulatorFunc,
Func<TAccumulate,TAccumulate,TAccumulate> combineAccumulatorsFunc, Func<TAccumulate,TResult>
resultSelector);
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
TResult
The type of the resulting value.
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seedFactory Func<TAccumulate>
A function that returns the initial accumulator value.
updateAccumulatorFunc Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element in a partition.
combineAccumulatorsFunc Func<TAccumulate,TAccumulate,TAccumulate>
An accumulator function to be invoked on the yielded accumulator result from each partition.
resultSelector Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or seedFactory or updateAccumulatorFunc or combineAccumulatorsFunc or resultSelector is a null
reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Remarks
This overload is specific to parallelized queries. A parallelized query may partition the data source sequence into several
sub-sequences (partitions). The updateAccumulatorFunc is invoked on each element within partitions. Each partition
then yields a single accumulated result. The combineAccumulatorsFunc is then invoked on the results of each partition
to yield a single element. This element is then transformed by the resultSelector function.
Aggregate<TSource,TAccumulate,TResult>
(ParallelQuery<TSource>, TAccumulate,
Func<TAccumulate,TSource,TAccumulate>,
Func<TAccumulate,TAccumulate,TAccumulate>,
Func<TAccumulate,TResult>)
Applies in parallel an accumulator function over a sequence. This overload is not available in the sequential
implementation.
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
TResult
The type of the resulting value.
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
updateAccumulatorFunc Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element in a partition.
combineAccumulatorsFunc Func<TAccumulate,TAccumulate,TAccumulate>
An accumulator function to be invoked on the yielded accumulator result from each partition.
resultSelector Func<TAccumulate,TResult>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or updateAccumulatorFunc or combineAccumulatorsFunc or resultSelector is a null reference (Nothing in
Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Remarks
This overload is specific to processing a parallelized query. A parallelized query may partition the data source sequence
into several sub-sequences (partitions). The updateAccumulatorFunc is invoked on each element within partitions.
Each partition then yields a single accumulated result. The combineAccumulatorsFunc is then invoked on the results of
each partition to yield a single element. This element is then transformed by the resultSelector function.
Aggregate<TSource,TAccumulate>(ParallelQuery<TSource>,
TAccumulate, Func<TAccumulate,TSource,TAccumulate>)
Applies in parallel an accumulator function over a sequence. The specified seed value is used as the initial accumulator
value.
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Func<TAccumulate,TSource,TAccumulate>
An accumulator function to be invoked on each element.
Returns
TAccumulate
The final accumulator value.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or func is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Aggregate<TSource>(ParallelQuery<TSource>,
Func<TSource,TSource,TSource>)
Applies in parallel an accumulator function over a sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence to aggregate over.
func Func<TSource,TSource,TSource>
An accumulator function to be invoked on each element.
Returns
TSource
The final accumulator value.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or func is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
ParallelEnumerable.All
I n this Article
Determines in parallel whether all elements of a sequence satisfy a condition.
public static bool All<TSource> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,bool>
predicate);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence whose elements to apply the predicate to.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty;
otherwise, false..
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Any
I n this Article
Overloads
Any<TSource>(ParallelQuery<TSource>)
Determines whether a parallel sequence contains any
elements.
Any<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Determines in parallel whether any element of a sequence
satisfies a condition.
Any<TSource>(ParallelQuery<TSource>)
Determines whether a parallel sequence contains any elements.
public static bool Any<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to check for emptiness.
Returns
Boolean
true if the source sequence contains any elements; otherwise, false.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
This overload of the Any method causes a fully-buffered merge to be performed before the result is returned. The
merge behavior can impact performance or cause an out of memory exception in large enumerables. To avoid the
merge behavior, use the overload of Any that takes a Func parameter, or else use an alternative method such as
FirstOrDefault.
Any<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Determines in parallel whether any element of a sequence satisfies a condition.
public static bool Any<TSource> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,bool>
predicate);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence to whose elements the predicate will be applied.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Boolean
true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.AsEnumerable
I n this Article
Converts a ParallelQuery<TSource> into an IEnumerable<T> to force sequential evaluation of the query.
public static System.Collections.Generic.IEnumerable<TSource> AsEnumerable<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to cast as IEnumerable<T>.
Returns
IEnumerable<TSource>
The input sequence typed as IEnumerable<T>.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
ParallelEnumerable.AsOrdered
I n this Article
Overloads
AsOrdered(ParallelQuery)
Enables treatment of a data source as if it were ordered,
overriding the default of unordered. AsOrdered may only be
invoked on non-generic sequences returned by AsParallel,
ParallelEnumerable.Range, and ParallelEnumerable.Repeat.
AsOrdered<TSource>(ParallelQuery<TSource>)
Enables treatment of a data source as if it were ordered,
overriding the default of unordered. AsOrdered may only be
invoked on generic sequences returned by AsParallel,
ParallelEnumerable.Range, and ParallelEnumerable.Repeat.
AsOrdered(ParallelQuery)
Enables treatment of a data source as if it were ordered, overriding the default of unordered. AsOrdered may only be
invoked on non-generic sequences returned by AsParallel, ParallelEnumerable.Range, and ParallelEnumerable.Repeat.
public static System.Linq.ParallelQuery AsOrdered (this System.Linq.ParallelQuery source);
Parameters
source ParallelQuery
The input sequence.
Returns
ParallelQuery
The source sequence which will maintain the original ordering in the subsequent query operators.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
InvalidOperationException
thrown if source contains no elements
-or-
if AsOrdered is called midway through a query. It is allowed to be called immediately after AsParallel(IEnumerable),
Range(Int32, Int32) or Repeat<TResult>(TResult, Int32).
Remarks
A natural tension exists between performance and preserving order in parallel processing. By default, a parallelized
query behaves as if the ordering of the results is arbitrary unless AsOrdered is applied or there is an explicit OrderBy
operator in the query. For more information, see Order Preservation in PLINQ.
AsOrdered<TSource>(ParallelQuery<TSource>)
AsOrdered<TSource>(ParallelQuery<TSource>)
Enables treatment of a data source as if it were ordered, overriding the default of unordered. AsOrdered may only be
invoked on generic sequences returned by AsParallel, ParallelEnumerable.Range, and ParallelEnumerable.Repeat.
public static System.Linq.ParallelQuery<TSource> AsOrdered<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The input sequence.
Returns
ParallelQuery<TSource>
The source sequence which will maintain the original ordering in the subsequent query operators.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
InvalidOperationException
Thrown if source contains no elements
-or-
if source is not one of AsParallel, ParallelEnumerable.Range, or ParallelEnumerable.Repeat.
Remarks
A natural tension exists between performance and preserving order in parallel processing. By default, a parallelized
query behaves as if the ordering of the results is arbitrary unless AsOrdered is applied or there is an explicit OrderBy
operator in the query. For more information, see Order Preservation in PLINQ.
ParallelEnumerable.AsParallel
I n this Article
Overloads
AsParallel(IEnumerable)
Enables parallelization of a query.
AsParallel<TSource>(Partitioner<TSource>)
Enables parallelization of a query, as sourced by a custom
partitioner that is responsible for splitting the input sequence
into partitions.
AsParallel<TSource>(IEnumerable<TSource>)
Enables parallelization of a query.
Remarks
This method binds the query to PLINQ. For more information, see Parallel LINQ (PLINQ).
AsParallel(IEnumerable)
Enables parallelization of a query.
public static System.Linq.ParallelQuery AsParallel (this System.Collections.IEnumerable source);
Parameters
source IEnumerable
An IEnumerable<T> to convert to a ParallelQuery.
Returns
ParallelQuery
The source as a ParallelQuery to bind to ParallelEnumerable extension methods.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
Remarks
The Cast operator can be used to convert a ParallelQuery to a ParallelQuery(T).
AsParallel<TSource>(Partitioner<TSource>)
Enables parallelization of a query, as sourced by a custom partitioner that is responsible for splitting the input sequence
into partitions.
public static System.Linq.ParallelQuery<TSource> AsParallel<TSource> (this
System.Collections.Concurrent.Partitioner<TSource> source);
Type Parameters
TSource
The type of elements of source .
Parameters
source Partitioner<TSource>
A partitioner over the input sequence.
Returns
ParallelQuery<TSource>
The source as a ParallelQuery to bind to ParallelEnumerable extension methods.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
Remarks
The source partitioner's GetOrderedPartitions method is used when ordering is enabled, whereas the partitioner's
GetPartitions is used if ordering is not enabled (the default). The source partitioner's GetDynamicPartitions and
GetDynamicOrderedPartitions are not used. For more information and examples, see Custom Partitioners for PLINQ
and TPL.
AsParallel<TSource>(IEnumerable<TSource>)
Enables parallelization of a query.
public static System.Linq.ParallelQuery<TSource> AsParallel<TSource> (this
System.Collections.Generic.IEnumerable<TSource> source);
Type Parameters
TSource
The type of elements of source .
Parameters
source IEnumerable<TSource>
An IEnumerable<T> to convert to a ParallelQuery<TSource>.
Returns
ParallelQuery<TSource>
The source as a ParallelQuery<TSource> to bind to ParallelEnumerable extension methods.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
ParallelEnumerable.AsSequential
I n this Article
Converts a ParallelQuery<TSource> into an IEnumerable<T> to force sequential evaluation of the query.
public static System.Collections.Generic.IEnumerable<TSource> AsSequential<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A ParallelQuery<TSource> to convert to an IEnumerable<T>.
Returns
IEnumerable<TSource>
The source as an IEnumerable<T> to bind to sequential extension methods.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
Remarks
For more information, see Order Preservation in PLINQ and How to: Control Ordering in a PLINQ Query.
ParallelEnumerable.AsUnordered
I n this Article
Allows an intermediate query to be treated as if no ordering is implied among the elements.
public static System.Linq.ParallelQuery<TSource> AsUnordered<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The input sequence.
Returns
ParallelQuery<TSource>
The source sequence with arbitrary order.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
Remarks
AsUnordered may provide performance benefits when ordering is not required in a portion of a query. By default,
PLINQ treats an input sequence as unordered unless OrderBy or AsOrdered is specified. However, if ordering was
turned on and is no longer needed, then AsUnordered can be used to turn it off in mid-query, and this may result in
performance benefits.The AsUnordered operator itself does not shuffle the source sequence; it simply removes the
ordering requirement for subsequent operators. If the source is ordered, subsequent operators might keep that
ordering if it is more efficient to do so. AsUnordered can be called anywhere in the query; it does not need to be called
immediately after AsParallel. For more information, see Understanding Speedup in PLINQ and Order Preservation in
PLINQ.
ParallelEnumerable.Average
I n this Article
Overloads
Average(ParallelQuery<Single>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Nullable<Int64>>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Nullable<Int32>>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Nullable<Double>>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Nullable<Single>>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Int64>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Int32>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Double>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Decimal>)
Computes in parallel the average of a sequence of values.
Average(ParallelQuery<Nullable<Decimal>>)
Computes in parallel the average of a sequence of values.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Single>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int64>>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int32>>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Double>>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Single>>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Int64>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Double>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Decimal>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Decimal>>) Computes in parallel the average of a sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Average(ParallelQuery<Single>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Single>
A sequence of values that are used to calculate an average.
Returns
Single
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Nullable<Int64>>)
Computes in parallel the average of a sequence of values.
public static Nullable<double> Average (this System.Linq.ParallelQuery<Nullable<long>> source);
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Double>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Nullable<Int32>>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Double>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Nullable<Double>>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Nullable<Double>>
The source sequence.
Returns
Nullable<Double>
Returns the average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
A sequence of values that are used to calculate an average.The average of the sequence of values. source is a null
reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Nullable<Single>>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Single>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Int64>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Int64>
A sequence of values that are used to calculate an average.
Returns
Double
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Int32>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Int32>
A sequence of values that are used to calculate an average.
Returns
Double
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Double>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Double>
A sequence of values that are used to calculate an average.
Returns
Double
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Decimal>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Decimal>
A sequence of values that are used to calculate an average.
Returns
Decimal
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average(ParallelQuery<Nullable<Decimal>>)
Computes in parallel the average of a sequence of values.
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values that are used to calculate an average.
Returns
Nullable<Decimal>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Single>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static float Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,float> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int64>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int32>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Double>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Single>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static Nullable<float> Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Int64>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Int32>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum or count of the elements in the sequence is larger than MaxValue. -or- One or more exceptions occurred
during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Double>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static double Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,double> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Decimal>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
Average<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Decimal>>)
Computes in parallel the average of a sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static Nullable<decimal> Average<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<decimal>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values that are used to calculate an average.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The average of the sequence of values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
OverflowException
(Thrown as inner exception in an AggregateException). The selector function returns a value greater than MaxValue
for the element type.
ParallelEnumerable.Cast
I n this Article
Converts the elements of a ParallelQuery to the specified type.
public static System.Linq.ParallelQuery<TResult> Cast<TResult> (this System.Linq.ParallelQuery
source);
Type Parameters
TResult
The type to convert the elements of source to.
Parameters
source ParallelQuery
The sequence that contains the elements to be converted.
Returns
ParallelQuery<TResult>
A sequence that contains each element of the source sequence converted to the specified type.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
InvalidCastException
The type of the source sequence could not be converted to TResult .
ParallelEnumerable.Concat
I n this Article
Overloads
Concat<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>) This Concat overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Concat<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>) Concatenates two parallel sequences.
Concat<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>)
This Concat overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Concat<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Concat with a left data source of type ParallelQuery<TSource> and a right
data source of type IEnumerable<T>. Otherwise, the Concat operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Concat<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>)
Concatenates two parallel sequences.
public static System.Linq.ParallelQuery<TSource> Concat<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
The first sequence to concatenate.
second ParallelQuery<TSource>
The sequence to concatenate to the first sequence.
Returns
ParallelQuery<TSource>
A sequence that contains the concatenated elements of the two input sequences.
Exceptions
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
ParallelEnumerable.Contains
I n this Article
Overloads
Contains<TSource>(ParallelQuery<TSource>, TSource,
IEqualityComparer<TSource>) Determines in parallel whether a sequence contains a specified
element by using a specified IEqualityComparer<T>.
Contains<TSource>(ParallelQuery<TSource>, TSource)
Determines in parallel whether a sequence contains a specified
element by using the default equality comparer.
Contains<TSource>(ParallelQuery<TSource>, TSource,
IEqualityComparer<TSource>)
Determines in parallel whether a sequence contains a specified element by using a specified IEqualityComparer<T>.
public static bool Contains<TSource> (this System.Linq.ParallelQuery<TSource> source, TSource value,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
comparer IEqualityComparer<TSource>
An equality comparer to compare values.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Contains<TSource>(ParallelQuery<TSource>, TSource)
Determines in parallel whether a sequence contains a specified element by using the default equality comparer.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence in which to locate a value.
value TSource
The value to locate in the sequence.
Returns
Boolean
true if the source sequence contains an element that has the specified value; otherwise, false.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Count
I n this Article
Overloads
Count<TSource>(ParallelQuery<TSource>)
Returns the number of elements in a parallel sequence.
Count<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Returns a number that represents how many elements in the
specified parallel sequence satisfy a condition.
Count<TSource>(ParallelQuery<TSource>)
Returns the number of elements in a parallel sequence.
public static int Count<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
Returns
Int32
The number of elements in the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The number of elements in source is larger than MaxValue. (In this case the InnerException is OverflowException) -or-
One or more exceptions occurred during the evaluation of the query.
Count<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns a number that represents how many elements in the specified parallel sequence satisfy a condition.
public static int Count<TSource> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,bool>
predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Int32
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
The number of elements in source is larger than MaxValue. (In this case the InnerException is OverflowException) -or-
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.DefaultIfEmpty
I n this Article
Overloads
DefaultIfEmpty<TSource>(ParallelQuery<TSource>)
Returns the elements of the specified parallel sequence or the
type parameter's default value in a singleton collection if the
sequence is empty.
DefaultIfEmpty<TSource>(ParallelQuery<TSource>, TSource)
Returns the elements of the specified parallel sequence or the
specified value in a singleton collection if the sequence is
empty.
DefaultIfEmpty<TSource>(ParallelQuery<TSource>)
Returns the elements of the specified parallel sequence or the type parameter's default value in a singleton collection if
the sequence is empty.
public static System.Linq.ParallelQuery<TSource> DefaultIfEmpty<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return a default value for if it is empty.
Returns
ParallelQuery<TSource>
A sequence that contains default(TSource) if source is empty; otherwise, source .
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
DefaultIfEmpty<TSource>(ParallelQuery<TSource>, TSource)
Returns the elements of the specified parallel sequence or the specified value in a singleton collection if the sequence is
empty.
public static System.Linq.ParallelQuery<TSource> DefaultIfEmpty<TSource> (this
System.Linq.ParallelQuery<TSource> source, TSource defaultValue);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the specified value for if it is empty.
defaultValue TSource
The value to return if the sequence is empty.
Returns
ParallelQuery<TSource>
A sequence that contains defaultValue if source is empty; otherwise, source .
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Distinct
I n this Article
Overloads
Distinct<TSource>(ParallelQuery<TSource>)
Returns distinct elements from a parallel sequence by using
the default equality comparer to compare values.
Distinct<TSource>(ParallelQuery<TSource>, IEquality
Comparer<TSource>) Returns distinct elements from a parallel sequence by using a
specified IEqualityComparer<T> to compare values.
Distinct<TSource>(ParallelQuery<TSource>)
Returns distinct elements from a parallel sequence by using the default equality comparer to compare values.
public static System.Linq.ParallelQuery<TSource> Distinct<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to remove duplicate elements from.
Returns
ParallelQuery<TSource>
A sequence that contains distinct elements from the source sequence.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Distinct<TSource>(ParallelQuery<TSource>,
IEqualityComparer<TSource>)
Returns distinct elements from a parallel sequence by using a specified IEqualityComparer<T> to compare values.
public static System.Linq.ParallelQuery<TSource> Distinct<TSource> (this
System.Linq.ParallelQuery<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>
comparer);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to remove duplicate elements from.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains distinct elements from the source sequence.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.ElementAt
I n this Article
Returns the element at a specified index in a parallel sequence.
public static TSource ElementAt<TSource> (this System.Linq.ParallelQuery<TSource> source, int
index);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
The element at the specified position in the source sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException
index is less than 0 or greater than or equal to the number of elements in source .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.ElementAtOrDefault
I n this Article
Returns the element at a specified index in a parallel sequence or a default value if the index is out of range.
public static TSource ElementAtOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
int index);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
default(TSource) if the index is outside the bounds of the source sequence; otherwise, the element at the specified
position in the source sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Empty
I n this Article
Returns an empty ParallelQuery{TResult} that has the specified type argument.
public static System.Linq.ParallelQuery<TResult> Empty<TResult> ();
Type Parameters
TResult
The type to assign to the type parameter of the returned generic sequence.
Returns
ParallelQuery<TResult>
An empty sequence whose type argument is TResult .
ParallelEnumerable.Except
I n this Article
Overloads
Except<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>) This Except overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Except<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>) Produces the set difference of two parallel sequences by using
the default equality comparer to compare values.
Except<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) This Except overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Except<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>, IEqualityComparer<TSource>) Produces the set difference of two parallel sequences by using
the specified IEqualityComparer<T> to compare values.
Except<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>)
This Except overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Except with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Except operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Except<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>)
Produces the set difference of two parallel sequences by using the default equality comparer to compare values.
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence whose elements that are not also in second will be returned.
second ParallelQuery<TSource>
A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the
returned sequence.
Returns
ParallelQuery<TSource>
A sequence that contains the set difference of the elements of two sequences.
Exceptions
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Except<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
This Except overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Except with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Except operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Except<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>, IEqualityComparer<TSource>)
Produces the set difference of two parallel sequences by using the specified IEqualityComparer<T> to compare values.
public static System.Linq.ParallelQuery<TSource> Except<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence whose elements that are not also in second will be returned.
second ParallelQuery<TSource>
A sequence whose elements that also occur in the first sequence will cause those elements to be removed from the
returned sequence.
comparer IEqualityComparer<TSource>
IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains the set difference of the elements of two sequences.
Exceptions
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.First
I n this Article
Overloads
First<TSource>(ParallelQuery<TSource>)
Returns the first element of a parallel sequence.
First<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Returns the first element in a parallel sequence that satisfies a
specified condition.
First<TSource>(ParallelQuery<TSource>)
Returns the first element of a parallel sequence.
public static TSource First<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the first element of.
Returns
TSource
The first element in the specified sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Remarks
If the query is not ordered, then the first element is non-deterministic. For more information, see Order Preservation in
PLINQ.
First<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns the first element in a parallel sequence that satisfies a specified condition.
public static TSource First<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
The first element in the sequence that passes the test in the specified predicate function.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
No element in source satisfies the condition in predicate .
Remarks
If the query is not ordered, then the first element is non-deterministic. For more information, see Order Preservation in
PLINQ.
ParallelEnumerable.FirstOrDefault
I n this Article
Overloads
FirstOrDefault<TSource>(ParallelQuery<TSource>)
Returns the first element of a parallel sequence, or a default
value if the sequence contains no elements.
FirstOrDefault<TSource>(ParallelQuery<TSource>, Func<
TSource,Boolean>) Returns the first element of the parallel sequence that satisfies
a condition or a default value if no such element is found.
FirstOrDefault<TSource>(ParallelQuery<TSource>)
Returns the first element of a parallel sequence, or a default value if the sequence contains no elements.
public static TSource FirstOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the first element of.
Returns
TSource
default(TSource) if source is empty; otherwise, the first element in source .
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
If the query is not ordered, then the first element is non-deterministic. For more information, see Order Preservation in
PLINQ.
FirstOrDefault<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns the first element of the parallel sequence that satisfies a condition or a default value if no such element is
found.
public static TSource FirstOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
default(TSource) if source is empty or if no element passes the test specified by predicate; otherwise, the first element
in source that passes the test specified by predicate.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
If the query is not ordered, then the first element is non-deterministic. For more information, see Order Preservation in
PLINQ.
ParallelEnumerable.ForAll
I n this Article
Invokes in parallel the specified action for each element in the source .
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The ParallelQuery<TSource> whose elements will be processed by action .
action Action<TSource>
An Action to invoke on each element.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
This is an efficient way to process the output from a parallelized query because it does not require a merge step at the
end. For more information, see Merge Options in PLINQ or Order Preservation in PLINQ. Because this method
produces a side-effect, it can be used to observe the actual order of execution in a parallel query.
ParallelEnumerable.GroupBy
I n this Article
Overloads
GroupBy<TSource,TKey,TElement,TResult>(ParallelQuery<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Groups the elements of a sequence according to a specified
Func<TKey,IEnumerable<TElement>,TResult>, IEquality key selector function and creates a result value from each
Comparer<TKey>) group and its key. Key values are compared by using a
specified comparer, and the elements of each group are
projected by using a specified function.
GroupBy<TSource,TKey,TElement,TResult>(ParallelQuery<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Groups in parallel the elements of a sequence according to a
Func<TKey,IEnumerable<TElement>,TResult>) specified key selector function and creates a result value from
each group and its key. The elements of each group are
projected by using a specified function.
GroupBy<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) Groups in parallel the elements of a sequence according to a
specified key selector function and projects the elements for
each group by using a specified function.
GroupBy<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>, IEquality Groups in parallel the elements of a sequence according to a
Comparer<TKey>) key selector function. The keys are compared by using a
comparer and each group's elements are projected by using a
specified function.
GroupBy<TSource,TKey,TResult>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>, Groups in parallel the elements of a sequence according to a
TResult>) specified key selector function and creates a result value from
each group and its key.
GroupBy<TSource,TKey,TResult>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>, Groups in parallel the elements of a sequence according to a
TResult>, IEqualityComparer<TKey>) specified key selector function and creates a result value from
each group and its key. The keys are compared by using a
specified comparer.
GroupBy<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>, IEqualityComparer<TKey>) Groups in parallel the elements of a sequence according to a
specified key selector function and compares the keys by using
a specified IComparer<T>.
GroupBy<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>) Groups in parallel the elements of a sequence according to a
specified key selector function.
GroupBy<TSource,TKey,TElement,TResult>
(ParallelQuery<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>,
Func<TKey,IEnumerable<TElement>,TResult>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. Key values are compared by using a specified comparer, and the elements of each group are
projected by using a specified function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in each IGrouping{TKey, TElement}.
TResult
The type of the result value returned by resultSelector .
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping{Key, TElement}.
resultSelector Func<TKey,IEnumerable<TElement>,TResult>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ParallelQuery<TResult>
A sequence of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey,TElement,TResult>
(ParallelQuery<TSource>, Func<TSource,TKey>,
Func<TSource,TElement>,
Func<TKey,IEnumerable<TElement>,TResult>)
Groups in parallel the elements of a sequence according to a specified key selector function and creates a result value
from each group and its key. The elements of each group are projected by using a specified function.
public static System.Linq.ParallelQuery<TResult> GroupBy<TSource,TKey,TElement,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement>
elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>
resultSelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in each IGrouping{TKey, TElement}.
TResult
The type of the result value returned by resultSelector .
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping.
resultSelector Func<TKey,IEnumerable<TElement>,TResult>
A function to create a result value from each group.
Returns
ParallelQuery<TResult>
A sequence of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>)
Groups in parallel the elements of a sequence according to a specified key selector function and projects the elements
for each group by using a specified function.
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in the IGrouping<TKey,TElement>.
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery<TSource> that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
Returns
ParallelQuery<IGrouping<TKey,TElement>>
A sequence of groups that are sorted descending according to TKey .
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Groups in parallel the elements of a sequence according to a key selector function. The keys are compared by using a
comparer and each group's elements are projected by using a specified function.
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the elements in the IGrouping
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery{TSource}that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
elementSelector Func<TSource,TElement>
A function to map each source element to an element in an IGrouping.
comparer IEqualityComparer<TKey>
An IComparer{TSource} to compare keys.
Returns
ParallelQuery<IGrouping<TKey,TElement>>
A sequence of groups that are sorted descending according to TKey .
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey,TResult>(ParallelQuery<TSource>,
Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>)
Groups in parallel the elements of a sequence according to a specified key selector function and creates a result value
from each group and its key.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TResult
The type of the result value returned by resultSelector .
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
resultSelector Func<TKey,IEnumerable<TSource>,TResult>
A function to create a result value from each group.
Returns
ParallelQuery<TResult>
A sequence of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey,TResult>(ParallelQuery<TSource>,
Func<TSource,TKey>,
Func<TKey,IEnumerable<TSource>,TResult>,
IEqualityComparer<TKey>)
Groups in parallel the elements of a sequence according to a specified key selector function and creates a result value
from each group and its key. The keys are compared by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TResult
The type of the result value returned by resultSelector .
Parameters
source ParallelQuery<TSource>
A sequence whose elements to group.
keySelector Func<TSource,TKey>
A function to extract the key for each element.
resultSelector Func<TKey,IEnumerable<TSource>,TResult>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ParallelQuery<TResult>
A sequence of groups.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>, IEqualityComparer<TKey>)
Groups in parallel the elements of a sequence according to a specified key selector function and compares the keys by
using a specified IComparer<T>.
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector >.
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery<TSource> that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IEqualityComparer<TKey>
An IComparer<T> to compare keys.
Returns
ParallelQuery<IGrouping<TKey,TSource>>
A sequence of groups that are sorted descending according to TKey .
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupBy<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>)
Groups in parallel the elements of a sequence according to a specified key selector function.
public static System.Linq.ParallelQuery<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey>
(this System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
An OrderedParallelQuery{TSource}that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
ParallelQuery<IGrouping<TKey,TSource>>
A sequence of groups that are sorted descending according to TKey .
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.GroupJoin
I n this Article
Overloads
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<
TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func< This GroupJoin overload should never be called. This method
TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>) is marked as obsolete and always throws
NotSupportedException when called.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<
TOuter>, ParallelQuery<TInner>, Func<TOuter,TKey>, Func< Correlates in parallel the elements of two sequences based on
TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>) equality of keys and groups the results. The default equality
comparer is used to compare keys.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<
TOuter>, IEnumerable<TInner>, Func<TOuter,TKey>, Func< This GroupJoin overload should never be called. This method
TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>, is marked as obsolete and always throws
IEqualityComparer<TKey>) NotSupportedException when called.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<
TOuter>, ParallelQuery<TInner>, Func<TOuter,TKey>, Func< Correlates in parallel the elements of two sequences based on
TInner,TKey>, Func<TOuter,IEnumerable<TInner>,TResult>, key equality and groups the results. A specified
IEqualityComparer<TKey>) IEqualityComparer<T> is used to compare keys.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,IEnumerable<TInner>,TResult>)
This GroupJoin overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector);
Type Parameters
TOuter
This type parameter is not used.
TInner
This type parameter is not used.
TKey
This type parameter is not used.
TResult
This type parameter is not used.
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
This overload exists to disallow usage of GroupJoin with a left data source of type ParallelQuery<TSource> and a right
data source of type IEnumerable<T>. Otherwise, the GroupJoin operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
ParallelQuery<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,IEnumerable<TInner>,TResult>)
Correlates in parallel the elements of two sequences based on equality of keys and groups the results. The default
equality comparer is used to compare keys.
public static System.Linq.ParallelQuery<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Linq.ParallelQuery<TInner> inner, Func<TOuter,TKey>
outerKeySelector, Func<TInner,TKey> innerKeySelector,
Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResult> resultSelector);
Type Parameters
TOuter
The type of the elements of the second sequence.
TInner
The type of the elements of the first sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing a grouped join on two sequences.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,IEnumerable<TInner>,TResult>,
IEqualityComparer<TKey>)
This GroupJoin overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
Type Parameters
TOuter
This type parameter is not used.
TInner
This type parameter is not used.
TKey
This type parameter is not used.
TResult
This type parameter is not used.
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
This parameter is not used.
comparer IEqualityComparer<TKey>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of GroupJoin with a left data source of type ParallelQuery<TSource> and a right
data source of type IEnumerable<T>. Otherwise, the GroupJoin operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
GroupJoin<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
ParallelQuery<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,IEnumerable<TInner>,TResult>,
IEqualityComparer<TKey>)
Correlates in parallel the elements of two sequences based on key equality and groups the results. A specified
IEqualityComparer<T> is used to compare keys.
Type Parameters
TOuter
The type of the elements of the second sequence.
TInner
The type of the elements of the first sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,IEnumerable<TInner>,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing a grouped join on two sequences.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Intersect
I n this Article
Overloads
Intersect<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) This Intersect overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Intersect<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>, IEqualityComparer<TSource>) Produces the set intersection of two parallel sequences by
using the specified IEqualityComparer{T} to compare values.
Intersect<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>) This Intersect overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Intersect<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>) Produces the set intersection of two parallel sequences by
using the default equality comparer to compare values.
Intersect<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
This Intersect overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Intersect<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Intersect with a left data source of type ParallelQuery<TSource> and a right
data source of type IEnumerable<T>. Otherwise, the Intersect operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Intersect<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>, IEqualityComparer<TSource>)
Produces the set intersection of two parallel sequences by using the specified IEqualityComparer{T} to compare values.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements that also appear in second will be returned.
second ParallelQuery<TSource>
A sequence whose distinct elements that also appear in the first sequence will be returned.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Intersect<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>)
This Intersect overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Intersect with a left data source of type ParallelQuery<TSource> and a right
data source of type IEnumerable<T>. Otherwise, the Intersect operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Intersect<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>)
Produces the set intersection of two parallel sequences by using the default equality comparer to compare values.
public static System.Linq.ParallelQuery<TSource> Intersect<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements that also appear in second will be returned.
second ParallelQuery<TSource>
A sequence whose distinct elements that also appear in the first sequence will be returned.
Returns
ParallelQuery<TSource>
A sequence that contains the elements that form the set intersection of two sequences.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Join
I n this Article
Overloads
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner, This Join overload should never be called. This method is
TKey>, Func<TOuter,TInner,TResult>) marked as obsolete and always throws
NotSupportedException when invoked.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
ParallelQuery<TInner>, Func<TOuter,TKey>, Func<TInner, Correlates in parallel the elements of two sequences based on
TKey>, Func<TOuter,TInner,TResult>) matching keys. The default equality comparer is used to
compare keys.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner, This Join overload should never be called. This method is
TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer< marked as obsolete and always throws
TKey>) NotSupportedException when invoked.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
ParallelQuery<TInner>, Func<TOuter,TKey>, Func<TInner, Correlates in parallel the elements of two sequences based on
TKey>, Func<TOuter,TInner,TResult>, IEqualityComparer< matching keys. A specified IEqualityComparer<T> is used to
TKey>) compare keys.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,TInner,TResult>)
This Join overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when invoked.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector, Func<TOuter,TInner,TResult>
resultSelector);
Type Parameters
TOuter
This type parameter is not used.
TInner
This type parameter is not used.
TKey
This type parameter is not used.
TResult
This type parameter is not used.
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,TInner,TResult>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
This overload exists to disallow usage Join with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Join operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
ParallelQuery<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,TInner,TResult>)
Correlates in parallel the elements of two sequences based on matching keys. The default equality comparer is used to
compare keys.
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Linq.ParallelQuery<TInner> inner, Func<TOuter,TKey>
outerKeySelector, Func<TInner,TKey> innerKeySelector, Func<TOuter,TInner,TResult> resultSelector);
Type Parameters
TOuter
The type of the elements of the second sequence.
TInner
The type of the elements of the first sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
IEnumerable<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)
This Join overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when invoked.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.ParallelQuery<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
Func<TOuter,TKey> outerKeySelector, Func<TInner,TKey> innerKeySelector, Func<TOuter,TInner,TResult>
resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TOuter
This type parameter is not used.
TInner
This type parameter is not used.
TKey
This type parameter is not used.
TResult
This type parameter is not used.
Parameters
outer ParallelQuery<TOuter>
This parameter is not used.
inner IEnumerable<TInner>
This parameter is not used.
outerKeySelector Func<TOuter,TKey>
This parameter is not used.
innerKeySelector Func<TInner,TKey>
This parameter is not used.
resultSelector Func<TOuter,TInner,TResult>
This parameter is not used.
comparer IEqualityComparer<TKey>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Join with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Join operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Join<TOuter,TInner,TKey,TResult>(ParallelQuery<TOuter>,
ParallelQuery<TInner>, Func<TOuter,TKey>, Func<TInner,TKey>,
Func<TOuter,TInner,TResult>, IEqualityComparer<TKey>)
Correlates in parallel the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is
used to compare keys.
Type Parameters
TOuter
The type of the elements of the second sequence.
TInner
The type of the elements of the first sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer ParallelQuery<TOuter>
The first sequence to join.
inner ParallelQuery<TInner>
The sequence to join to the first sequence.
outerKeySelector Func<TOuter,TKey>
A function to extract the join key from each element of the first sequence.
innerKeySelector Func<TInner,TKey>
A function to extract the join key from each element of the second sequence.
resultSelector Func<TOuter,TInner,TResult>
A function to create a result element from two matching elements.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing an inner join on two sequences.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or action is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Last
I n this Article
Overloads
Last<TSource>(ParallelQuery<TSource>)
Returns the last element of a parallel sequence.
Last<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Returns the last element of a parallel sequence that satisfies a
specified condition.
Last<TSource>(ParallelQuery<TSource>)
Returns the last element of a parallel sequence.
public static TSource Last<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the last element from.
Returns
TSource
The value at the last position in the source sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Remarks
If the query is not ordered, then the last element is non-deterministic. For more information, see Order Preservation in
PLINQ.
Last<TSource>(ParallelQuery<TSource>, Func<TSource,Boolean>)
Returns the last element of a parallel sequence that satisfies a specified condition.
public static TSource Last<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
The last element in the sequence that passes the test in the specified predicate function.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
No element in source satisfies the condition in predicate .
Remarks
If the query is not ordered, then the last element is non-deterministic. For more information, see Order Preservation in
PLINQ.
ParallelEnumerable.LastOrDefault
I n this Article
Overloads
LastOrDefault<TSource>(ParallelQuery<TSource>, Func<
TSource,Boolean>) Returns the last element of a parallel sequence that satisfies a
condition, or a default value if no such element is found.
LastOrDefault<TSource>(ParallelQuery<TSource>)
Returns the last element of a parallel sequence, or a default
value if the sequence contains no elements.
LastOrDefault<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns the last element of a parallel sequence that satisfies a condition, or a default value if no such element is found.
public static TSource LastOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
TSource
default() if the sequence is empty or if no elements pass the test in the predicate function; otherwise, the last element
that passes the test in the predicate function.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
If the query is not ordered, then the last element is non-deterministic. For more information, see Order Preservation in
PLINQ.
LastOrDefault<TSource>(ParallelQuery<TSource>)
Returns the last element of a parallel sequence, or a default value if the sequence contains no elements.
public static TSource LastOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return an element from.
Returns
TSource
default() if the source sequence is empty; otherwise, the last element in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
If the query is not ordered, then the last element is non-deterministic. For more information, see Order Preservation in
PLINQ.
ParallelEnumerable.LongCount
I n this Article
Overloads
LongCount<TSource>(ParallelQuery<TSource>)
Returns an Int64 that represents the total number of elements
in a parallel sequence.
LongCount<TSource>(ParallelQuery<TSource>, Func<
TSource,Boolean>) Returns an Int64 that represents how many elements in a
parallel sequence satisfy a condition.
LongCount<TSource>(ParallelQuery<TSource>)
Returns an Int64 that represents the total number of elements in a parallel sequence.
public static long LongCount<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
Returns
Int64
The number of elements in the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The number of elements in source is larger than MaxValue. -or- One or more exceptions occurred during the evaluation
of the query.
OverflowException
The computed result is greater than MaxValue.
LongCount<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns an Int64 that represents how many elements in a parallel sequence satisfy a condition.
public static long LongCount<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence that contains elements to be counted.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
Int64
A number that represents how many elements in the sequence satisfy the condition in the predicate function.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
The number of elements in source is larger than MaxValue. -or- One or more exceptions occurred during the evaluation
of the query.
OverflowException
The computed result is greater than MaxValue.
ParallelEnumerable.Max
I n this Article
Overloads
Max(ParallelQuery<Nullable<Single>>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Single>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Nullable<Int64>>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Nullable<Int32>>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Nullable<Double>>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Double>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Int64>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Int32>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Decimal>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Nullable<Decimal>>)
Returns the maximum value in a parallel sequence of values.
Max<TSource,TResult>(ParallelQuery<TSource>, Func<
TSource,TResult>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int64>>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Single>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Single>>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int32>>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Double>>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Decimal>>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Int64>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Double>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,
Decimal>) Invokes in parallel a transform function on each element of a
sequence and returns the maximum value.
Max<TSource>(ParallelQuery<TSource>)
Returns the maximum value in a parallel sequence of values.
Max(ParallelQuery<Nullable<Single>>)
Returns the maximum value in a parallel sequence of values.
public static Nullable<float> Max (this System.Linq.ParallelQuery<Nullable<float>> source);
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Single>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Single>)
Returns the maximum value in a parallel sequence of values.
public static float Max (this System.Linq.ParallelQuery<float> source);
Parameters
source ParallelQuery<Single>
A sequence of values to determine the maximum value of.
Returns
Single
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Nullable<Int64>>)
Returns the maximum value in a parallel sequence of values.
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Int64>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Nullable<Int32>>)
Returns the maximum value in a parallel sequence of values.
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Int32>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Nullable<Double>>)
Returns the maximum value in a parallel sequence of values.
public static Nullable<double> Max (this System.Linq.ParallelQuery<Nullable<double>> source);
Parameters
source ParallelQuery<Nullable<Double>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Double>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Double>)
Returns the maximum value in a parallel sequence of values.
Parameters
source ParallelQuery<Double>
A sequence of values to determine the maximum value of.
Returns
Double
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Int64>)
Returns the maximum value in a parallel sequence of values.
Returns the maximum value in a parallel sequence of values.
public static long Max (this System.Linq.ParallelQuery<long> source);
Parameters
source ParallelQuery<Int64>
A sequence of values to determine the maximum value of.
Returns
Int64
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Int32>)
Returns the maximum value in a parallel sequence of values.
Parameters
source ParallelQuery<Int32>
A sequence of values to determine the maximum value of.
Returns
Int32
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Decimal>)
Returns the maximum value in a parallel sequence of values.
Parameters
source ParallelQuery<Decimal>
A sequence of values to determine the maximum value of.
Returns
Decimal
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max(ParallelQuery<Nullable<Decimal>>)
Returns the maximum value in a parallel sequence of values.
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values to determine the maximum value of.
Returns
Nullable<Decimal>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,TResult>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
TResult
The type of the value returned by selector .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Max<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int64>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Int64>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
public static float Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,float> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Max<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Single>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int32>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Int32>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Double>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
public static Nullable<double> Max<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<double>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Decimal>>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Int64
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
public static int Max<TSource> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,int>
selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Int32
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Max<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Max<TSource>(ParallelQuery<TSource>,
Func<TSource,Decimal>)
Invokes in parallel a transform function on each element of a sequence and returns the maximum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Max<TSource>(ParallelQuery<TSource>)
Returns the maximum value in a parallel sequence of values.
Type Parameters
TSource
The type of elements in the source sequence.
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the maximum value of.
Returns
TSource
The maximum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
ParallelEnumerable.Min
I n this Article
Overloads
Min(ParallelQuery<Single>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Nullable<Single>>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Nullable<Int64>>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Nullable<Int32>>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Nullable<Double>>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Nullable<Decimal>>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Int64>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Int32>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Double>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Decimal>)
Returns the minimum value in a parallel sequence of values.
Min<TSource,TResult>(ParallelQuery<TSource>, Func<
TSource,TResult>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Single>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Single>>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int32>>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Double>>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int64>>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Int64>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Decimal>>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Double>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,
Decimal>) Invokes in parallel a transform function on each element of a
sequence and returns the minimum value.
Min<TSource>(ParallelQuery<TSource>)
Returns the minimum value in a parallel sequence of values.
Min(ParallelQuery<Single>)
Returns the minimum value in a parallel sequence of values.
public static float Min (this System.Linq.ParallelQuery<float> source);
Parameters
source ParallelQuery<Single>
A sequence of values to determine the minimum value of.
Returns
Single
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min(ParallelQuery<Nullable<Single>>)
Returns the minimum value in a parallel sequence of values.
public static Nullable<float> Min (this System.Linq.ParallelQuery<Nullable<float>> source);
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Single>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min(ParallelQuery<Nullable<Int64>>)
Returns the minimum value in a parallel sequence of values.
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Int64>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min(ParallelQuery<Nullable<Int32>>)
Returns the minimum value in a parallel sequence of values.
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Int32>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min(ParallelQuery<Nullable<Double>>)
Returns the minimum value in a parallel sequence of values.
public static Nullable<double> Min (this System.Linq.ParallelQuery<Nullable<double>> source);
Parameters
source ParallelQuery<Nullable<Double>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Double>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min(ParallelQuery<Nullable<Decimal>>)
Returns the minimum value in a parallel sequence of values.
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values to determine the minimum value of.
Returns
Nullable<Decimal>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min(ParallelQuery<Int64>)
Returns the minimum value in a parallel sequence of values.
Returns the minimum value in a parallel sequence of values.
public static long Min (this System.Linq.ParallelQuery<long> source);
Parameters
source ParallelQuery<Int64>
A sequence of values to determine the minimum value of.
Returns
Int64
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min(ParallelQuery<Int32>)
Returns the minimum value in a parallel sequence of values.
Parameters
source ParallelQuery<Int32>
A sequence of values to determine the minimum value of.
Returns
Int32
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min(ParallelQuery<Double>)
Returns the minimum value in a parallel sequence of values.
Parameters
source ParallelQuery<Double>
A sequence of values to determine the minimum value of.
Returns
Double
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min(ParallelQuery<Decimal>)
Returns the minimum value in a parallel sequence of values.
Parameters
source ParallelQuery<Decimal>
A sequence of values to determine the minimum value of.
Returns
Decimal
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,TResult>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
Type Parameters
TSource
The type of elements of source .
TResult
The type of the value returned by selector .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,TResult>
A transform function to apply to each element.
Returns
TResult
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Single>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
public static Nullable<float> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int32>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Int32>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Double>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int64>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
public static Nullable<long> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<long>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Int64>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
public static long Min<TSource> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,long>
selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Int64
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
public static int Min<TSource> (this System.Linq.ParallelQuery<TSource> source, Func<TSource,int>
selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Int32
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Decimal>>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
public static Nullable<decimal> Min<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<decimal>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource>(ParallelQuery<TSource>, Func<TSource,Decimal>)
Invokes in parallel a transform function on each element of a sequence and returns the minimum value.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
Min<TSource>(ParallelQuery<TSource>)
Returns the minimum value in a parallel sequence of values.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to determine the minimum value of.
Returns
TSource
The minimum value in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source contains no elements and TSource is a non-nullable value type.
ParallelEnumerable.OfType
I n this Article
Filters the elements of a ParallelQuery based on a specified type.
public static System.Linq.ParallelQuery<TResult> OfType<TResult> (this System.Linq.ParallelQuery
source);
Type Parameters
TResult
The type to filter the elements of the sequence on.
Parameters
source ParallelQuery
The sequence whose elements to filter.
Returns
ParallelQuery<TResult>
A sequence that contains elements from the input sequence of type .
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.OrderBy
I n this Article
Overloads
OrderBy<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>) Sorts in parallel the elements of a sequence in ascending order
according to a key.
OrderBy<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>, IComparer<TKey>) Sorts in parallel the elements of a sequence in ascending order
by using a specified comparer.
OrderBy<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>)
Sorts in parallel the elements of a sequence in ascending order according to a key.
public static System.Linq.OrderedParallelQuery<TSource> OrderBy<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. To achieve a stable sort, change a query of the
form: var ordered = source.Select((e,i) => new { E=e, I=i }).OrderBy((v) => v.e).ThenBy(v => v.i).Select((v) => v.e);
OrderBy<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>, IComparer<TKey>)
Sorts in parallel the elements of a sequence in ascending order by using a specified comparer.
public static System.Linq.OrderedParallelQuery<TSource> OrderBy<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
ParallelEnumerable.OrderByDescending
I n this Article
Overloads
OrderByDescending<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>) Sorts in parallel the elements of a sequence in descending
order according to a key.
OrderByDescending<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>, IComparer<TKey>) Sorts the elements of a sequence in descending order by using
a specified comparer.
OrderByDescending<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>)
Sorts in parallel the elements of a sequence in descending order according to a key.
public static System.Linq.OrderedParallelQuery<TSource> OrderByDescending<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted descending according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
OrderByDescending<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>, IComparer<TKey>)
Sorts the elements of a sequence in descending order by using a specified comparer.
public static System.Linq.OrderedParallelQuery<TSource> OrderByDescending<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
A sequence of values to order.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted descending according to a key.
Exceptions
ArgumentNullException
source or KeySelector is a null reference (Nothing in Visual Basic)..
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
ParallelEnumerable.Range
I n this Article
Generates a parallel sequence of integral numbers within a specified range.
public static System.Linq.ParallelQuery<int> Range (int start, int count);
Parameters
start Int32
The value of the first integer in the sequence.
count Int32
The number of sequential integers to generate.
Returns
ParallelQuery<Int32>
An IEnumerable in C# or IEnumerable(Of Int32) in Visual Basic that contains a range of sequential integral numbers.
Exceptions
ArgumentOutOfRangeException
count is less than 0 -or- start + count - 1 is larger than MaxValue.
ParallelEnumerable.Repeat
I n this Article
Generates a parallel sequence that contains one repeated value.
public static System.Linq.ParallelQuery<TResult> Repeat<TResult> (TResult element, int count);
Type Parameters
TResult
The type of the value to be repeated in the result sequence.
Parameters
element TResult
The value to be repeated.
count Int32
The number of times to repeat the value in the generated sequence.
Returns
ParallelQuery<TResult>
A sequence that contains a repeated value.
Exceptions
ArgumentOutOfRangeException
count is less than 0.
ParallelEnumerable.Reverse
I n this Article
Inverts the order of the elements in a parallel sequence.
public static System.Linq.ParallelQuery<TSource> Reverse<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to reverse.
Returns
ParallelQuery<TSource>
A sequence whose elements correspond to those of the input sequence in reverse order.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Select
I n this Article
Overloads
Select<TSource,TResult>(ParallelQuery<TSource>, Func<
TSource,Int32,TResult>) Projects in parallel each element of a sequence into a new
form by incorporating the element's index.
Select<TSource,TResult>(ParallelQuery<TSource>, Func<
TSource,TResult>) Projects in parallel each element of a sequence into a new
form.
Select<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,Int32,TResult>)
Projects in parallel each element of a sequence into a new form by incorporating the element's index.
public static System.Linq.ParallelQuery<TResult> Select<TSource,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,TResult> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of elements resturned by selector.
Parameters
source ParallelQuery<TSource>
A sequence of values to invoke a transform function on.
selector Func<TSource,Int32,TResult>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the transform function on each element of source , based on the
index supplied to selector .
Exceptions
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
OverflowException
More than MaxValue elements are enumerated by the query. This condition might occur in streaming scenarios.
Select<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,TResult>)
Projects in parallel each element of a sequence into a new form.
public static System.Linq.ParallelQuery<TResult> Select<TSource,TResult> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TResult> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of elements resturned by selector.
Parameters
source ParallelQuery<TSource>
A sequence of values to invoke a transform function on.
selector Func<TSource,TResult>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the transform function on each element of source .
Exceptions
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.SelectMany
I n this Article
Overloads
SelectMany<TSource,TCollection,TResult>(ParallelQuery<
TSource>, Func<TSource,IEnumerable<TCollection>>, Func< Projects each element of a sequence to an IEnumerable<T>,
TSource,TCollection,TResult>) flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein.
SelectMany<TSource,TCollection,TResult>(ParallelQuery<
TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Projects each element of a sequence to an IEnumerable<T>,
Func<TSource,TCollection,TResult>) flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein. The
index of each source element is used in the intermediate
projected form of that element.
SelectMany<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,IEnumerable<TResult>>) Projects in parallel each element of a sequence to an
IEnumerable<T> and flattens the resulting sequences into one
sequence.
SelectMany<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,Int32,IEnumerable<TResult>>) Projects in parallel each element of a sequence to an
IEnumerable<T>, and flattens the resulting sequences into
one sequence. The index of each source element is used in the
projected form of that element.
SelectMany<TSource,TCollection,TResult>
(ParallelQuery<TSource>,
Func<TSource,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein.
public static System.Linq.ParallelQuery<TResult> SelectMany<TSource,TCollection,TResult> (this
System.Linq.ParallelQuery<TSource> source,
Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector,
Func<TSource,TCollection,TResult> resultSelector);
Type Parameters
TSource
The type of the intermediate elements collected by collectionSelector .
TCollection
The type of elements of source .
TResult
The type of elements in the result sequence.
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
collectionSelector Func<TSource,IEnumerable<TCollection>>
A transform function to apply to each source element; the second parameter of the function represents the index of the
source element.
resultSelector Func<TSource,TCollection,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on
each element of source based on the index supplied to collectionSelector , and then mapping each of those
sequence elements and their corresponding source element to a result element.
Exceptions
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
More than MaxValue elements are enumerated by the query.
AggregateException
One or more exceptions occurred during the evaluation of the query.
SelectMany<TSource,TCollection,TResult>
(ParallelQuery<TSource>,
Func<TSource,Int32,IEnumerable<TCollection>>,
Func<TSource,TCollection,TResult>)
Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and
invokes a result selector function on each element therein. The index of each source element is used in the intermediate
projected form of that element.
Type Parameters
TSource
The type of the intermediate elements collected by collectionSelector .
TCollection
The type of elements of source .
TResult
The type of elements to return.
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
collectionSelector Func<TSource,Int32,IEnumerable<TCollection>>
A transform function to apply to each source element; the second parameter of the function represents the index of the
source element.
resultSelector Func<TSource,TCollection,TResult>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function collectionSelector on
each element of source based on the index supplied to collectionSelector , and then mapping each of those
sequence elements and their corresponding source element to a result element.
Exceptions
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
More than MaxValue elements are enumerated by the query.
AggregateException
One or more exceptions occurred during the evaluation of the query.
SelectMany<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,IEnumerable<TResult>>)
Projects in parallel each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one
sequence.
SelectMany<TSource,TResult>(ParallelQuery<TSource>,
Func<TSource,Int32,IEnumerable<TResult>>)
Projects in parallel each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into one
sequence. The index of each source element is used in the projected form of that element.
Type Parameters
TSource
The type of elements of source .
TResult
The type of the elements of the sequence returned by selector.
Parameters
source ParallelQuery<TSource>
A sequence of values to project.
selector Func<TSource,Int32,IEnumerable<TResult>>
A transform function to apply to each element.
Returns
ParallelQuery<TResult>
A sequence whose elements are the result of invoking the one-to-many transform function on each element of the
input sequence.
Exceptions
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
More than MaxValue elements are enumerated by the query.
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.SequenceEqual
I n this Article
Overloads
SequenceEqual<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>) This SequenceEqual overload should never be called. This
method is marked as obsolete and always throws
NotSupportedException when called.
SequenceEqual<TSource>(ParallelQuery<TSource>, Parallel
Query<TSource>) Determines whether two parallel sequences are equal by
comparing the elements by using the default equality
comparer for their type.
SequenceEqual<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) This SequenceEqual overload should never be called. This
method is marked as obsolete and always throws
NotSupportedException when called.
SequenceEqual<TSource>(ParallelQuery<TSource>, Parallel
Query<TSource>, IEqualityComparer<TSource>) Determines whether two parallel sequences are equal by
comparing their elements by using a specified
IEqualityComparer{T}.
SequenceEqual<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>)
This SequenceEqual overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
Boolean
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
Thrown every time this method is called.
Remarks
This overload exists to disallow usage of SequenceEqual with a left data source of type ParallelQuery<TSource> and a
right data source of type IEnumerable<T>. Otherwise, the SequenceEqual operator would appear to be binding to the
parallel implementation, but would in reality bind to the sequential implementation.
SequenceEqual<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>)
Determines whether two parallel sequences are equal by comparing the elements by using the default equality
comparer for their type.
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Linq.ParallelQuery<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence to compare to second.
second ParallelQuery<TSource>
A sequence to compare to the first input sequence.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the
default equality comparer for their type; otherwise, false.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
SequenceEqual<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
This SequenceEqual overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
Boolean
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
Thrown every time this method is called.
Remarks
This overload exists to disallow usage of SequenceEqual with a left data source of type ParallelQuery<TSource> and a
right data source of type IEnumerable<T>. Otherwise, the SequenceEqual operator would appear to be binding to the
parallel implementation, but would in reality bind to sequential implementation.
SequenceEqual<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>, IEqualityComparer<TSource>)
Determines whether two parallel sequences are equal by comparing their elements by using a specified
IEqualityComparer{T}.
public static bool SequenceEqual<TSource> (this System.Linq.ParallelQuery<TSource> first,
System.Linq.ParallelQuery<TSource> second, System.Collections.Generic.IEqualityComparer<TSource>
comparer);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence to compare to second .
second ParallelQuery<TSource>
A sequence to compare to the first input sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to use to compare elements.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements are equal according to the
default equality comparer for their type; otherwise, false.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Single
I n this Article
Overloads
Single<TSource>(ParallelQuery<TSource>)
Returns the only element of a parallel sequence, and throws
an exception if there is not exactly one element in the
sequence.
Single<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Returns the only element of a parallel sequence that satisfies a
specified condition, and throws an exception if more than one
such element exists.
Single<TSource>(ParallelQuery<TSource>)
Returns the only element of a parallel sequence, and throws an exception if there is not exactly one element in the
sequence.
public static TSource Single<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
Returns
TSource
The single element of the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
The input sequence contains more than one element. -or- The input sequence is empty.
Single<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns the only element of a parallel sequence that satisfies a specified condition, and throws an exception if more
than one such element exists.
public static TSource Single<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
predicate Func<TSource,Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies a condition.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
No element satisfies the condition in predicate . -or- More than one element satisfies the condition in predicate .
ParallelEnumerable.SingleOrDefault
I n this Article
Overloads
SingleOrDefault<TSource>(ParallelQuery<TSource>)
Returns the only element of a parallel sequence, or a default
value if the sequence is empty; this method throws an
exception if there is more than one element in the sequence.
SingleOrDefault<TSource>(ParallelQuery<TSource>, Func<
TSource,Boolean>) Returns the only element of a parallel sequence that satisfies a
specified condition or a default value if no such element exists;
this method throws an exception if more than one element
satisfies the condition.
SingleOrDefault<TSource>(ParallelQuery<TSource>)
Returns the only element of a parallel sequence, or a default value if the sequence is empty; this method throws an
exception if there is more than one element in the sequence.
public static TSource SingleOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
Returns
TSource
The single element of the input sequence, or default() if the sequence contains no elements.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
SingleOrDefault<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns the only element of a parallel sequence that satisfies a specified condition or a default value if no such element
exists; this method throws an exception if more than one element satisfies the condition.
public static TSource SingleOrDefault<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return the single element of.
predicate Func<TSource,Boolean>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition, or default() if no such element is found.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
InvalidOperationException
source is empty or multiple elements are returned.
ParallelEnumerable.Skip
I n this Article
Bypasses a specified number of elements in a parallel sequence and then returns the remaining elements.
public static System.Linq.ParallelQuery<TSource> Skip<TSource> (this
System.Linq.ParallelQuery<TSource> source, int count);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
count Int32
The number of elements to skip before returning the remaining elements.
Returns
ParallelQuery<TSource>
A sequence that contains the elements that occur after the specified index in the input sequence.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
Count is greater than MaxValue
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
If count > source.Count(), no elements are returned.
ParallelEnumerable.SkipWhile
I n this Article
Overloads
SkipWhile<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Bypasses elements in a parallel sequence as long as a specified
condition is true and then returns the remaining elements.
SkipWhile<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32,Boolean>) Bypasses elements in a parallel sequence as long as a specified
condition is true and then returns the remaining elements. The
element's index is used in the logic of the predicate function.
SkipWhile<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Bypasses elements in a parallel sequence as long as a specified condition is true and then returns the remaining
elements.
public static System.Linq.ParallelQuery<TSource> SkipWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,bool> predicate);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from the input sequence starting at the first element in the linear series that
does not pass the test specified by predicate.
Exceptions
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
SkipWhile<TSource>(ParallelQuery<TSource>,
Func<TSource,Int32,Boolean>)
Bypasses elements in a parallel sequence as long as a specified condition is true and then returns the remaining
elements. The element's index is used in the logic of the predicate function.
public static System.Linq.ParallelQuery<TSource> SkipWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,bool> predicate);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Int32,Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the
source element.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from the input sequence starting at the first element in the linear series that
does not pass the test specified by predicate.
Exceptions
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
More than MaxValue elements are enumerated by the query.
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Sum
I n this Article
Overloads
Sum(ParallelQuery<Nullable<Single>>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Nullable<Int64>>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Nullable<Int32>>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Nullable<Double>>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Single>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Int64>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Int32>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Double>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Decimal>)
Computes in parallel the sum of a sequence of values.
Sum(ParallelQuery<Nullable<Decimal>>)
Computes in parallel the sum of a sequence of values.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Single>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Decimal>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Double>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Int64>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Decimal>>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Double>>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int32>>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Int64>>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,
Nullable<Single>>) Computes in parallel the sum of the sequence of values that
are obtained by invoking a transform function on each
element of the input sequence.
Sum(ParallelQuery<Nullable<Single>>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Nullable<Single>>
A sequence of values to calculate the sum of.
Returns
Nullable<Single>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Nullable<Int64>>)
Computes in parallel the sum of a sequence of values.
public static Nullable<long> Sum (this System.Linq.ParallelQuery<Nullable<long>> source);
Parameters
source ParallelQuery<Nullable<Int64>>
A sequence of values to calculate the sum of.
Returns
Nullable<Int64>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Nullable<Int32>>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Nullable<Int32>>
A sequence of values to calculate the sum of.
Returns
Nullable<Int32>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Nullable<Double>>)
Computes in parallel the sum of a sequence of values.
public static Nullable<double> Sum (this System.Linq.ParallelQuery<Nullable<double>> source);
Parameters
source ParallelQuery<Nullable<Double>>
A sequence of values to calculate the sum of.
Returns
Nullable<Double>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Single>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Single>
A sequence of values to calculate the sum of.
Returns
Single
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Int64>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Int64>
A sequence of values to calculate the sum of.
Returns
Int64
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Int32>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Int32>
A sequence of values to calculate the sum of.
Returns
Int32
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Double>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Double>
A sequence of values to calculate the sum of.
Returns
Double
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Decimal>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Decimal>
A sequence of values to calculate the sum of.
Returns
Decimal
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum(ParallelQuery<Nullable<Decimal>>)
Computes in parallel the sum of a sequence of values.
Parameters
source ParallelQuery<Nullable<Decimal>>
A sequence of values to calculate the sum of.
Returns
Nullable<Decimal>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Single>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Single>
A transform function to apply to each element.
Returns
Single
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>,
Func<TSource,Decimal>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Decimal>
A transform function to apply to each element.
Returns
Decimal
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Double>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Double>
A transform function to apply to each element.
Returns
Double
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Int32>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements in the source sequence.
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Int32>
A transform function to apply to each element.
Returns
Int32
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>, Func<TSource,Int64>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Int64>
A transform function to apply to each element.
Returns
Int64
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Decimal>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Decimal>>
A transform function to apply to each element.
Returns
Nullable<Decimal>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Double>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static Nullable<double> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<double>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Double>>
A transform function to apply to each element.
Returns
Nullable<Double>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int32>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static Nullable<int> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<int>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Int32>>
A transform function to apply to each element.
Returns
Nullable<Int32>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Int64>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Int64>>
A transform function to apply to each element.
Returns
Nullable<Int64>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
Sum<TSource>(ParallelQuery<TSource>,
Func<TSource,Nullable<Single>>)
Computes in parallel the sum of the sequence of values that are obtained by invoking a transform function on each
element of the input sequence.
public static Nullable<float> Sum<TSource> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,Nullable<float>> selector);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A sequence of values to calculate the sum of.
selector Func<TSource,Nullable<Single>>
A transform function to apply to each element.
Returns
Nullable<Single>
The sum of the projected values in the sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or selector is a null reference (Nothing in Visual Basic).
AggregateException
The sum is larger than MaxValue. -or- One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Take
I n this Article
Returns a specified number of contiguous elements from the start of a parallel sequence.
public static System.Linq.ParallelQuery<TSource> Take<TSource> (this
System.Linq.ParallelQuery<TSource> source, int count);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
count Int32
The number of elements to return.
Returns
ParallelQuery<TSource>
A sequence that contains the specified number of elements from the start of the input sequence.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.TakeWhile
I n this Article
Overloads
TakeWhile<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32,Boolean>) Returns elements from a parallel sequence as long as a
specified condition is true. The element's index is used in the
logic of the predicate function.
TakeWhile<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Returns elements from a parallel sequence as long as a
specified condition is true.
TakeWhile<TSource>(ParallelQuery<TSource>,
Func<TSource,Int32,Boolean>)
Returns elements from a parallel sequence as long as a specified condition is true. The element's index is used in the
logic of the predicate function.
public static System.Linq.ParallelQuery<TSource> TakeWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,bool> predicate);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Int32,Boolean>
A function to test each source element for a condition; the second parameter of the function represents the index of the
source element.
Returns
ParallelQuery<TSource>
A sequence that contains elements from the input sequence that occur before the element at which the test no longer
passes.
Exceptions
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
More than MaxValue elements are enumerated by this query.
AggregateException
One or more exceptions occurred during the evaluation of the query.
TakeWhile<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Returns elements from a parallel sequence as long as a specified condition is true.
public static System.Linq.ParallelQuery<TSource> TakeWhile<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,bool> predicate);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
The sequence to return elements from.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from the input sequence that occur before the element at which the test no
longer passes.
Exceptions
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.ThenBy
I n this Article
Overloads
ThenBy<TSource,TKey>(OrderedParallelQuery<TSource>,
Func<TSource,TKey>) Performs in parallel a subsequent ordering of the elements in
a sequence in ascending order according to a key.
ThenBy<TSource,TKey>(OrderedParallelQuery<TSource>,
Func<TSource,TKey>, IComparer<TKey>) Performs in parallel a subsequent ordering of the elements in
a sequence in ascending order by using a specified comparer.
ThenBy<TSource,TKey>(OrderedParallelQuery<TSource>,
Func<TSource,TKey>)
Performs in parallel a subsequent ordering of the elements in a sequence in ascending order according to a key.
public static System.Linq.OrderedParallelQuery<TSource> ThenBy<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
ThenBy<TSource,TKey>(OrderedParallelQuery<TSource>,
Func<TSource,TKey>, IComparer<TKey>)
Performs in parallel a subsequent ordering of the elements in a sequence in ascending order by using a specified
comparer.
public static System.Linq.OrderedParallelQuery<TSource> ThenBy<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
ParallelEnumerable.ThenByDescending
I n this Article
Overloads
ThenByDescending<TSource,TKey>(OrderedParallelQuery<
TSource>, Func<TSource,TKey>) Performs in parallel a subsequent ordering of the elements in
a sequence in descending order, according to a key.
ThenByDescending<TSource,TKey>(OrderedParallelQuery<
TSource>, Func<TSource,TKey>, IComparer<TKey>) Performs in parallel a subsequent ordering of the elements in
a sequence in descending order by using a specified comparer.
ThenByDescending<TSource,TKey>
(OrderedParallelQuery<TSource>, Func<TSource,TKey>)
Performs in parallel a subsequent ordering of the elements in a sequence in descending order, according to a key.
public static System.Linq.OrderedParallelQuery<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
Returns
OrderedParallelQuery<TSource>
A sequence whose elements are sorted descending according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
ThenByDescending<TSource,TKey>
(OrderedParallelQuery<TSource>, Func<TSource,TKey>,
IComparer<TKey>)
Performs in parallel a subsequent ordering of the elements in a sequence in descending order by using a specified
comparer.
public static System.Linq.OrderedParallelQuery<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.OrderedParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source OrderedParallelQuery<TSource>
An OrderedParallelQuery{TSource} that contains elements to sort.
keySelector Func<TSource,TKey>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer{TKey} to compare keys.
Returns
OrderedParallelQuery<TSource>
A sequence whose elements are sorted descending according to a key.
Exceptions
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
In contrast to the sequential implementation, this is not a stable sort. See the remarks for OrderBy for an approach to
implementing a stable sort.
ParallelEnumerable.ToArray
I n this Article
Creates an array from a ParallelQuery<TSource>.
public static TSource[] ToArray<TSource> (this System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence to create an array from.
Returns
TSource[]
An array that contains the elements from the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.ToDictionary
I n this Article
Overloads
ToDictionary<TSource,TKey,TElement>(ParallelQuery<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>) Creates a Dictionary<TKey,TValue> from a
ParallelQuery<TSource> according to specified key selector
and element selector functions.
ToDictionary<TSource,TKey,TElement>(ParallelQuery<
TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Creates a Dictionary<TKey,TValue> from a
IEqualityComparer<TKey>) ParallelQuery<TSource> according to a specified key selector
function, a comparer, and an element selector function.
ToDictionary<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>) Creates a Dictionary<TKey,TValue> from a
ParallelQuery<TSource> according to a specified key selector
function.
ToDictionary<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>, IEqualityComparer<TKey>) Creates a Dictionary<TKey,TValue> from a
ParallelQuery<TSource> according to a specified key selector
function and key comparer.
ToDictionary<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>)
Creates a Dictionary<TKey,TValue> from a ParallelQuery<TSource> according to specified key selector and element
selector functions.
public static System.Collections.Generic.Dictionary<TKey,TElement>
ToDictionary<TSource,TKey,TElement> (this System.Linq.ParallelQuery<TSource> source,
Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector or elementSelector is a null reference (Nothing in Visual Basic).
AggregateException
keySelector produces a key that is a null reference (Nothing in Visual Basic). -or- keySelector produces duplicate
keys for two elements. -or- One or more exceptions occurred during the evaluation of the query.
ToDictionary<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Creates a Dictionary<TKey,TValue> from a ParallelQuery<TSource> according to a specified key selector function, a
comparer, and an element selector function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
Dictionary<TKey,TElement>
A Dictionary<TKey,TValue> that contains values of type TElement selected from the input sequence
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector or elementSelector is a null reference (Nothing in Visual Basic).
AggregateException
keySelector produces a key that is a null reference (Nothing in Visual Basic). -or- keySelector produces duplicate
keys for two elements. -or- One or more exceptions occurred during the evaluation of the query.
ToDictionary<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>)
Creates a Dictionary<TKey,TValue> from a ParallelQuery<TSource> according to a specified key selector function.
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
Returns
Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
AggregateException
keySelector produces a key that is a null reference (Nothing in Visual Basic). -or- keySelector produces duplicate
keys for two elements. -or- One or more exceptions occurred during the evaluation of the query.
ToDictionary<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>, IEqualityComparer<TKey>)
Creates a Dictionary<TKey,TValue> from a ParallelQuery<TSource> according to a specified key selector function and
key comparer.
public static System.Collections.Generic.Dictionary<TKey,TSource> ToDictionary<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
A sequence to create a Dictionary<TKey,TValue> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
Dictionary<TKey,TSource>
A Dictionary<TKey,TValue> that contains keys and values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
AggregateException
keySelector produces a key that is a null reference (Nothing in Visual Basic). -or- keySelector produces duplicate
keys for two elements. -or- One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.ToList
I n this Article
Creates a List<T> from an ParallelQuery<TSource>.
public static System.Collections.Generic.List<TSource> ToList<TSource> (this
System.Linq.ParallelQuery<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source ParallelQuery<TSource>
A sequence to create a List<T> from.
Returns
List<TSource>
A List<T> that contains elements from the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.ToLookup
I n this Article
Overloads
ToLookup<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>, IEquality Creates an ILookup<TKey,TElement> from a
Comparer<TKey>) ParallelQuery<TSource> according to a specified key selector
function, a comparer and an element selector function.
ToLookup<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>) Creates an ILookup<TKey,TElement> from a
ParallelQuery<TSource> according to specified key selector
and element selector functions.
ToLookup<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>, IEqualityComparer<TKey>) Creates an ILookup<TKey,TElement> from a
ParallelQuery<TSource> according to a specified key selector
function and key comparer.
ToLookup<TSource,TKey>(ParallelQuery<TSource>, Func<
TSource,TKey>) Creates an ILookup<TKey,TElement> from a
ParallelQuery<TSource> according to a specified key selector
function.
ToLookup<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>,
IEqualityComparer<TKey>)
Creates an ILookup<TKey,TElement> from a ParallelQuery<TSource> according to a specified key selector function, a
comparer and an element selector function.
public static System.Linq.ILookup<TKey,TElement> ToLookup<TSource,TKey,TElement> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement>
elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ILookup<TKey,TElement>
A Lookup<(Of <(TKey, TElement>)>) that contains values of type TElement selected from the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector or elementSelector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ToLookup<TSource,TKey,TElement>(ParallelQuery<TSource>,
Func<TSource,TKey>, Func<TSource,TElement>)
Creates an ILookup<TKey,TElement> from a ParallelQuery<TSource> according to specified key selector and element
selector functions.
public static System.Linq.ILookup<TKey,TElement> ToLookup<TSource,TKey,TElement> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement>
elementSelector);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
TElement
The type of the value returned by elementSelector .
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
elementSelector Func<TSource,TElement>
A transform function to produce a result element value from each element.
Returns
ILookup<TKey,TElement>
A ILookup<TKey,TElement> that contains values of type TElement selected from the input sequence.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector or elementSelector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ToLookup<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>, IEqualityComparer<TKey>)
Creates an ILookup<TKey,TElement> from a ParallelQuery<TSource> according to a specified key selector function
and key comparer.
public static System.Linq.ILookup<TKey,TSource> ToLookup<TSource,TKey> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,TKey> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
ILookup<TKey,TSource>
A ILookup<TKey,TElement> that contains keys and values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector or is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ToLookup<TSource,TKey>(ParallelQuery<TSource>,
Func<TSource,TKey>)
Creates an ILookup<TKey,TElement> from a ParallelQuery<TSource> according to a specified key selector function.
Type Parameters
TSource
The type of elements of source .
TKey
The type of the key returned by keySelector .
Parameters
source ParallelQuery<TSource>
The sequence to create a ILookup<TKey,TElement> from.
keySelector Func<TSource,TKey>
A function to extract a key from each element.
Returns
ILookup<TKey,TSource>
A ILookup<TKey,TElement> that contains keys and values.
Exceptions
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
ArgumentNullException
source or keySelector is a null reference (Nothing in Visual Basic).
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Union
I n this Article
Overloads
Union<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>) This Union overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Union<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>) Produces the set union of two parallel sequences by using the
default equality comparer.
Union<TSource>(ParallelQuery<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) This Union overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when called.
Union<TSource>(ParallelQuery<TSource>, ParallelQuery<
TSource>, IEqualityComparer<TSource>) Produces the set union of two parallel sequences by using a
specified IEqualityComparer{T}.
Union<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>)
This Union overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
This overload exists to disallow usage of Union with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Union operator would appear to be binding to the parallel
implementation, but would in reality bind to sequential implementation.
Union<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>)
Produces the set union of two parallel sequences by using the default equality comparer.
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Linq.ParallelQuery<TSource> second);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements form the first set for the union.
second ParallelQuery<TSource>
A sequence whose distinct elements form the second set for the union.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from both input sequences, excluding duplicates.
Exceptions
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Union<TSource>(ParallelQuery<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
This Union overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when called.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TSource> Union<TSource> (this
System.Linq.ParallelQuery<TSource> first, System.Collections.Generic.IEnumerable<TSource> second,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
This type parameter is not used.
Parameters
first ParallelQuery<TSource>
This parameter is not used.
second IEnumerable<TSource>
This parameter is not used.
comparer IEqualityComparer<TSource>
This parameter is not used.
Returns
ParallelQuery<TSource>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Remarks
This overload exists to disallow usage of Union with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Union operator would appear to be binding to the parallel
implementation, but would in reality bind to the sequential implementation.
Union<TSource>(ParallelQuery<TSource>,
ParallelQuery<TSource>, IEqualityComparer<TSource>)
Produces the set union of two parallel sequences by using a specified IEqualityComparer{T}.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
first ParallelQuery<TSource>
A sequence whose distinct elements form the first set for the union.
second ParallelQuery<TSource>
A sequence whose distinct elements form the second set for the union.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
ParallelQuery<TSource>
A sequence that contains the elements from both input sequences, excluding duplicates.
Exceptions
ArgumentNullException
first or second is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.Where
I n this Article
Overloads
Where<TSource>(ParallelQuery<TSource>, Func<TSource,
Boolean>) Filters in parallel a sequence of values based on a predicate.
Where<TSource>(ParallelQuery<TSource>, Func<TSource,
Int32,Boolean>) Filters in parallel a sequence of values based on a predicate.
Each element's index is used in the logic of the predicate
function.
Where<TSource>(ParallelQuery<TSource>,
Func<TSource,Boolean>)
Filters in parallel a sequence of values based on a predicate.
public static System.Linq.ParallelQuery<TSource> Where<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,bool> predicate);
Type Parameters
TSource
The type of the elements of source.
Parameters
source ParallelQuery<TSource>
A sequence to filter.
predicate Func<TSource,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains elements from the input sequence that satisfy the condition.
Exceptions
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
Where<TSource>(ParallelQuery<TSource>,
Where<TSource>(ParallelQuery<TSource>,
Func<TSource,Int32,Boolean>)
Filters in parallel a sequence of values based on a predicate. Each element's index is used in the logic of the predicate
function.
public static System.Linq.ParallelQuery<TSource> Where<TSource> (this
System.Linq.ParallelQuery<TSource> source, Func<TSource,int,bool> predicate);
Type Parameters
TSource
The type of the elements of source.
Parameters
source ParallelQuery<TSource>
A sequence to filter.
predicate Func<TSource,Int32,Boolean>
A function to test each element for a condition.
Returns
ParallelQuery<TSource>
A sequence that contains elements from the input sequence that satisfy the condition.
Exceptions
ArgumentNullException
source or predicate is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
OverflowException
More than MaxValue elements are enumerated by the query.
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelEnumerable.WithCancellation
I n this Article
Sets the CancellationToken to associate with the query.
public static System.Linq.ParallelQuery<TSource> WithCancellation<TSource> (this
System.Linq.ParallelQuery<TSource> source, System.Threading.CancellationToken cancellationToken);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the option.
cancellationToken CancellationToken
A cancellation token.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, but with the registered cancellation token.
Exceptions
ObjectDisposedException
The CancellationTokenSource associated with the cancellationToken has been disposed.
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
InvalidOperationException
WithCancellation is used multiple times in the query.
ParallelEnumerable.WithDegreeOfParallelism
I n this Article
Sets the degree of parallelism to use in a query. Degree of parallelism is the maximum number of concurrently
executing tasks that will be used to process the query.
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the limit on the degrees of parallelism.
degreeOfParallelism Int32
The degree of parallelism for the query. The default value is Math.Min(ProcessorCount, MAX_SUPPORTED_DOP ) where
MAX_SUPPORTED_DOP is 512.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, with the limit on the degrees of parallelism set.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException
degreeOfParallelism is less than 1 or greater than 511.
InvalidOperationException
WithDegreeOfParallelism is used multiple times in the query.
ParallelEnumerable.WithExecutionMode
I n this Article
Sets the execution mode of the query.
public static System.Linq.ParallelQuery<TSource> WithExecutionMode<TSource> (this
System.Linq.ParallelQuery<TSource> source, System.Linq.ParallelExecutionMode executionMode);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the option.
executionMode ParallelExecutionMode
The mode in which to execute the query.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, but with the registered execution mode.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
ArgumentException
executionMode is not a valid ParallelExecutionMode value.
InvalidOperationException
WithExecutionMode is used multiple times in the query.
Remarks
The execution mode specifies whether PLINQ will attempt to fall back to sequential execution if it detects certain query
shapes. For more information and examples, see How to: Specify the Execution Mode in PLINQ.
ParallelEnumerable.WithMergeOptions
I n this Article
Sets the merge options for this query, which specify how the query will buffer output.
public static System.Linq.ParallelQuery<TSource> WithMergeOptions<TSource> (this
System.Linq.ParallelQuery<TSource> source, System.Linq.ParallelMergeOptions mergeOptions);
Type Parameters
TSource
The type of elements of source .
Parameters
source ParallelQuery<TSource>
A ParallelQuery on which to set the option.
mergeOptions ParallelMergeOptions
The merge options to set for this query.
Returns
ParallelQuery<TSource>
ParallelQuery representing the same query as source, but with the registered merge options.
Exceptions
ArgumentNullException
source is a null reference (Nothing in Visual Basic).
ArgumentException
mergeOptions is not a valid ParallelMergeOptions value.
InvalidOperationException
WithMergeOptions is used multiple times in the query.
ParallelEnumerable.Zip
I n this Article
Overloads
Zip<TFirst,TSecond,TResult>(ParallelQuery<TFirst>,
IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>) This Zip overload should never be called. This method is
marked as obsolete and always throws
NotSupportedException when invoked.
Zip<TFirst,TSecond,TResult>(ParallelQuery<TFirst>, Parallel
Query<TSecond>, Func<TFirst,TSecond,TResult>) Merges in parallel two sequences by using the specified
predicate function.
Zip<TFirst,TSecond,TResult>(ParallelQuery<TFirst>,
IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)
This Zip overload should never be called. This method is marked as obsolete and always throws
NotSupportedException when invoked.
[System.Obsolete("The second data source of a binary operator must be of type
System.Linq.ParallelQuery<T> rather than System.Collections.Generic.IEnumerable<T>. To fix this
problem, use the AsParallel() extension method to convert the right data source to
System.Linq.ParallelQuery<T>.")]
public static System.Linq.ParallelQuery<TResult> Zip<TFirst,TSecond,TResult> (this
System.Linq.ParallelQuery<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second,
Func<TFirst,TSecond,TResult> resultSelector);
Type Parameters
TFirst
This type parameter is not used.
TSecond
This type parameter is not used.
TResult
This type parameter is not used.
Parameters
first ParallelQuery<TFirst>
This parameter is not used.
second IEnumerable<TSecond>
This parameter is not used.
resultSelector Func<TFirst,TSecond,TResult>
This parameter is not used.
Returns
ParallelQuery<TResult>
This overload always throws a NotSupportedException.
Attributes ObsoleteAttribute
Exceptions
NotSupportedException
The exception that occurs when this method is called.
Remarks
This overload exists to disallow usage of Zip with a left data source of type ParallelQuery<TSource> and a right data
source of type IEnumerable<T>. Otherwise, the Zip operator would appear to be bind to the parallel implementation,
but would in reality bind to the sequential implementation.
Zip<TFirst,TSecond,TResult>(ParallelQuery<TFirst>,
ParallelQuery<TSecond>, Func<TFirst,TSecond,TResult>)
Merges in parallel two sequences by using the specified predicate function.
Type Parameters
TFirst
The type of the elements of the first sequence.
TSecond
The type of the elements of the second sequence.
TResult
The type of the return elements.
Parameters
first ParallelQuery<TFirst>
The first sequence to zip.
second ParallelQuery<TSecond>
The second sequence to zip.
resultSelector Func<TFirst,TSecond,TResult>
A function to create a result element from two matching elements.
Returns
ParallelQuery<TResult>
A sequence that has elements of type TResult that are obtained by performing resultSelector pairwise on two
sequences. If the sequence lengths are unequal, this truncates to the length of the shorter sequence.
Exceptions
ArgumentNullException
first or second or resultSelector is a null reference (Nothing in Visual Basic).
OperationCanceledException
The query was canceled with the token passed in through WithCancellation .
AggregateException
One or more exceptions occurred during the evaluation of the query.
ParallelExecutionMode Enum
The query execution mode is a hint that specifies how the system should handle performance trade-offs when
parallelizing queries.
D eclaration
public enum ParallelExecutionMode
I nheritance H ierarchy
Object
ValueType
Enum
Fields
Default This is the default setting. PLINQ will examine the query's structure and will only parallelize the query if
will likely result in speedup. If the query structure indicates that speedup is not likely to be obtained,
then PLINQ will execute the query as an ordinary LINQ to Objects query.
ForceParallelism Parallelize the entire query, even if that means using high-overhead algorithms. Use this flag in cases
where you know that parallel execution of the query will result in speedup, but PLINQ in the Default
mode would execute it as sequential.
ParallelExecutionMode.Default
I n this Article
This is the default setting. PLINQ will examine the query's structure and will only parallelize the query if will likely result
in speedup. If the query structure indicates that speedup is not likely to be obtained, then PLINQ will execute the query
as an ordinary LINQ to Objects query.
Default
Returns
ParallelExecutionMode
ParallelExecutionMode.ForceParallelism
I n this Article
Parallelize the entire query, even if that means using high-overhead algorithms. Use this flag in cases where you know
that parallel execution of the query will result in speedup, but PLINQ in the Default mode would execute it as sequential.
ForceParallelism
Returns
ParallelExecutionMode
ParallelMergeOptions Enum
Specifies the preferred type of output merge to use in a query. In other words, it indicates how PLINQ should merge the
results from the various partitions back into a single result sequence. This is a hint only, and may not be respected by
the system when parallelizing all queries.
D eclaration
public enum ParallelMergeOptions
I nheritance H ierarchy
Object
ValueType
Enum
Remarks
Use NotBuffered for queries that will be consumed and output as streams, this has the lowest latency between
beginning query execution and elements being yielded. For some queries, such as those involving a sort (OrderBy,
OrderByDescending), buffering is essential and a hint of NotBuffered or AutoBuffered will be ignored. However, queries
that are created by using the AsOrdered operator can be streamed as long as no further sorting is performed within the
query itself.
Use AutoBuffered for most cases; this is the default. It strikes a balance between latency and overall performance.
Use FullyBuffered for queries when the entire output can be processed before the information is needed. This option
offers the best performance when all of the output can be accumulated before yielding any information, though it is not
suitable for stream processing or showing partial results mid-query.
Fields
AutoBuffered Use a merge with output buffers of a size chosen by the system. Results will accumulate into an output
buffer before they are available to the consumer of the query.
FullyBuffered Use a merge with full output buffers. The system will accumulate all of the results before making any of
them available to the consumer of the query.
NotBuffered Use a merge without output buffers. As soon as result elements have been computed, make that element
available to the consumer of the query.
ParallelMergeOptions.AutoBuffered
I n this Article
Use a merge with output buffers of a size chosen by the system. Results will accumulate into an output buffer before
they are available to the consumer of the query.
AutoBuffered
Returns
ParallelMergeOptions
ParallelMergeOptions.Default
I n this Article
Use the default merge type, which is AutoBuffered.
Default
Returns
ParallelMergeOptions
ParallelMergeOptions.FullyBuffered
I n this Article
Use a merge with full output buffers. The system will accumulate all of the results before making any of them available
to the consumer of the query.
FullyBuffered
Returns
ParallelMergeOptions
ParallelMergeOptions.NotBuffered
I n this Article
Use a merge without output buffers. As soon as result elements have been computed, make that element available to
the consumer of the query.
NotBuffered
Returns
ParallelMergeOptions
ParallelQuery Class
Represents a parallel sequence.
D eclaration
public class ParallelQuery : System.Collections.IEnumerable
I nheritance H ierarchy
Object
IEnumerable.GetEnumerator()
Returns
IEnumerator
An enumerator that iterates through the sequence.
ParallelQuery<TSource> Class
Represents a parallel sequence.
D eclaration
public class ParallelQuery<TSource> : System.Linq.ParallelQuery,
System.Collections.Generic.IEnumerable<TSource>
I nheritance H ierarchy
Object
ParallelQuery
Methods
GetEnumerator()
Returns
System.Collections.Generic.IEnumerator<TSource>
An enumerator that iterates through the sequence.
ParallelQuery<TSource>.GetEnumerator
I n this Article
Returns an enumerator that iterates through the sequence.
public virtual System.Collections.Generic.IEnumerator<TSource> GetEnumerator ();
Returns
System.Collections.Generic.IEnumerator<TSource>
An enumerator that iterates through the sequence.
Queryable Class
Provides a set of static ( Shared in Visual Basic) methods for querying data structures that implement
IQueryable<T>.
D eclaration
public static class Queryable
I nheritance H ierarchy
Object
Remarks
The set of methods declared in the Queryable class provides an implementation of the standard query operators for
querying data sources that implement IQueryable<T>. The standard query operators are general purpose methods that
follow the LINQ pattern and enable you to express traversal, filter, and projection operations over data in any .NET-
based programming language.
The majority of the methods in this class are defined as extension methods that extend the IQueryable<T> type. This
means they can be called like an instance method on any object that implements IQueryable<T>. These methods that
extend IQueryable<T> do not perform any querying directly. Instead, their functionality is to build an Expression object,
which is an expression tree that represents the cumulative query. The methods then pass the new expression tree to
either the Execute<TResult>(Expression) method or the CreateQuery<TElement>(Expression) method of the input
IQueryable<T>. The method that is called depends on whether the Queryable method returns a singleton value, in
which case Execute<TResult>(Expression) is called, or has enumerable results, in which case CreateQuery<TElement>
(Expression) is called.
The actual query execution on the target data is performed by a class that implements IQueryable<T>. The expectation
of any IQueryable<T> implementation is that the result of executing an expression tree that was constructed by a
Queryable standard query operator method is equivalent to the result of calling the corresponding method in the
Enumerable class, if the data source were an IEnumerable<T>.
In addition to the standard query operator methods that operate on IQueryable<T> objects, this class also contains a
method, AsQueryable, which types IEnumerable objects as IQueryable objects.
Methods
Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value,
and the specified function is used to select the result value.
D eclaration
public static TResult Aggregate<TSource,TAccumulate,TResult> (this
System.Linq.IQueryable<TSource> source, TAccumulate seed,
System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>> func,
System.Linq.Expressions.Expression<Func<TAccumulate,TResult>> selector);
Parameters
source IQueryable<TSource>
A sequence to aggregate over.
seed TAccumulate
seed TAccumulate
The initial accumulator value.
func Expression<Func<TAccumulate,TSource,TAccumulate>>
An accumulator function to invoke on each element.
selector Expression<Func<TAccumulate,TResult>>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
D eclaration
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Linq.IQueryable<TSource>
source, TAccumulate seed,
System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>> func);
Parameters
source IQueryable<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Expression<Func<TAccumulate,TSource,TAccumulate>>
An accumulator function to invoke on each element.
Returns
TAccumulate
The final accumulator value.
Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>)
Parameters
source IQueryable<TSource>
A sequence to aggregate over.
func Expression<Func<TSource,TSource,TSource>>
An accumulator function to apply to each element.
Returns
TSource
The final accumulator value.
All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Parameters
source IQueryable<TSource>
A sequence whose elements to test for a condition.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false .
Any<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
A sequence to check for being empty.
Returns
Boolean
true if the source sequence contains any elements; otherwise, false .
Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Parameters
source IQueryable<TSource>
A sequence whose elements to test for a condition.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Boolean
true if any elements in the source sequence pass the test in the specified predicate; otherwise, false .
Append<TSource>(IQueryable<TSource>, TSource)
D eclaration
public static System.Linq.IQueryable<TSource> Append<TSource> (this
System.Linq.IQueryable<TSource> source, TSource element);
Parameters
source IQueryable<TSource>
element TSource
Returns
IQueryable<TSource>
AsQueryable(IEnumerable)
Parameters
source IEnumerable
A sequence to convert.
Returns
IQueryable
An IQueryable that represents the input sequence.
AsQueryable<TElement>(IEnumerable<TElement>)
Parameters
source IEnumerable<TElement>
A sequence to convert.
Returns
IQueryable<TElement>
An IQueryable<T> that represents the input sequence.
Average(IQueryable<Single>)
Parameters
source IQueryable<Single>
A sequence of Single values to calculate the average of.
Returns
Single
The average of the sequence of values.
Average(IQueryable<Nullable<Single>>)
Parameters
source IQueryable<Nullable<Single>>
A sequence of nullable Single values to calculate the average of.
Returns
Nullable<Single>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average(IQueryable<Nullable<Int64>>)
Parameters
source IQueryable<Nullable<Int64>>
A sequence of nullable Int64 values to calculate the average of.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average(IQueryable<Nullable<Double>>)
Parameters
source IQueryable<Nullable<Double>>
A sequence of nullable Double values to calculate the average of.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average(IQueryable<Nullable<Int32>>)
Parameters
source IQueryable<Nullable<Int32>>
A sequence of nullable Int32 values to calculate the average of.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average(IQueryable<Int64>)
Parameters
source IQueryable<Int64>
A sequence of Int64 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Average(IQueryable<Int32>)
Parameters
source IQueryable<Int32>
A sequence of Int32 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Average(IQueryable<Double>)
Parameters
source IQueryable<Double>
A sequence of Double values to calculate the average of.
Returns
Double
The average of the sequence of values.
Average(IQueryable<Decimal>)
Parameters
source IQueryable<Decimal>
A sequence of Decimal values to calculate the average of.
Returns
Decimal
The average of the sequence of values.
Average(IQueryable<Nullable<Decimal>>)
Parameters
source IQueryable<Nullable<Decimal>>
A sequence of nullable Decimal values to calculate the average of.
Returns
Nullable<Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>)
Computes the average of a sequence of Single values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static float Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,float>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Single>>
A projection function to apply to each element.
Returns
Single
The average of the sequence of values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>)
Computes the average of a sequence of nullable Single values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<float> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<float>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Single>>>
A projection function to apply to each element.
Returns
Nullable<Single>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>)
Computes the average of a sequence of nullable Int64 values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<long>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Int64>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>)
Computes the average of a sequence of nullable Int32 values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<int>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Int32>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>)
Computes the average of a sequence of nullable Double values that is obtained by invoking a projection function
on each element of the input sequence.
D eclaration
public static Nullable<double> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<double>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Double>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>)
Computes the average of a sequence of Int64 values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,long>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Int64>>
A projection function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)
Computes the average of a sequence of Int32 values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Int32>>
A projection function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>)
Computes the average of a sequence of Double values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static double Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,double>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Double>>
A projection function to apply to each element.
Returns
Double
The average of the sequence of values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>)
Computes the average of a sequence of Decimal values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static decimal Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,decimal>> selector);
Parameters
source IQueryable<TSource>
A sequence of values that are used to calculate an average.
selector Expression<Func<TSource,Decimal>>
A projection function to apply to each element.
Returns
Decimal
The average of the sequence of values.
Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>)
Computes the average of a sequence of nullable Decimal values that is obtained by invoking a projection function
on each element of the input sequence.
D eclaration
public static Nullable<decimal> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<decimal>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Decimal>>>
A projection function to apply to each element.
Returns
Nullable<Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Cast<TResult>(IQueryable)
Parameters
source IQueryable
The IQueryable that contains the elements to be converted.
Returns
IQueryable<TResult>
An IQueryable<T> that contains each element of the source sequence converted to the specified type.
Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Parameters
source1 IQueryable<TSource>
The first sequence to concatenate.
source2 IEnumerable<TSource>
The sequence to concatenate to the first sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the concatenated elements of the two input sequences.
Parameters
source IQueryable<TSource>
An IQueryable<T> in which to locate item .
item TSource
The object to locate in the sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
Boolean
true if the input sequence contains an element that has the specified value; otherwise, false .
Contains<TSource>(IQueryable<TSource>, TSource)
Determines whether a sequence contains a specified element by using the default equality comparer.
D eclaration
public static bool Contains<TSource> (this System.Linq.IQueryable<TSource> source, TSource
item);
Parameters
source IQueryable<TSource>
An IQueryable<T> in which to locate item .
item TSource
The object to locate in the sequence.
Returns
Boolean
true if the input sequence contains an element that has the specified value; otherwise, false .
Count<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
The IQueryable<T> that contains the elements to be counted.
Returns
Int32
The number of elements in the input sequence.
Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Returns the number of elements in the specified sequence that satisfies a condition.
D eclaration
public static int Count<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Int32
The number of elements in the sequence that satisfies the condition in the predicate function.
DefaultIfEmpty<TSource>(IQueryable<TSource>)
Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the
sequence is empty.
D eclaration
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource> (this
System.Linq.IQueryable<TSource> source);
Parameters
source IQueryable<TSource>
The IQueryable<T> to return a default value for if empty.
Returns
IQueryable<TSource>
An IQueryable<T> that contains default ( TSource ) if source is empty; otherwise, source .
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)
Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is
empty.
D eclaration
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource> (this
System.Linq.IQueryable<TSource> source, TSource defaultValue);
Parameters
source IQueryable<TSource>
The IQueryable<T> to return the specified value for if empty.
defaultValue TSource
The value to return if the sequence is empty.
Returns
IQueryable<TSource>
An IQueryable<T> that contains defaultValue if source is empty; otherwise, source .
Distinct<TSource>(IQueryable<TSource>)
Returns distinct elements from a sequence by using the default equality comparer to compare values.
D eclaration
public static System.Linq.IQueryable<TSource> Distinct<TSource> (this
System.Linq.IQueryable<TSource> source);
Parameters
source IQueryable<TSource>
The IQueryable<T> to remove duplicates from.
Returns
IQueryable<TSource>
An IQueryable<T> that contains distinct elements from source .
Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>)
Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Linq.IQueryable<TSource> Distinct<TSource> (this
System.Linq.IQueryable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>
comparer);
Parameters
source IQueryable<TSource>
The IQueryable<T> to remove duplicates from.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains distinct elements from source .
ElementAt<TSource>(IQueryable<TSource>, Int32)
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
The element at the specified position in source .
ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32)
Returns the element at a specified index in a sequence or a default value if the index is out of range.
D eclaration
public static TSource ElementAtOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
int index);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
default ( TSource ) if index is outside the bounds of source ; otherwise, the element at the specified position in source .
Produces the set difference of two sequences by using the specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Linq.IQueryable<TSource> Except<TSource> (this
System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource>
source2, System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements that are not also in source2 will be returned.
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will not appear in the returned sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the set difference of the two sequences.
Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Produces the set difference of two sequences by using the default equality comparer to compare values.
D eclaration
public static System.Linq.IQueryable<TSource> Except<TSource> (this
System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource>
source2);
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements that are not also in source2 will be returned.
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will not appear in the returned sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the set difference of the two sequences.
First<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
The IQueryable<T> to return the first element of.
Returns
TSource
The first element in source .
First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
TSource
The first element in source that passes the test in predicate .
FirstOrDefault<TSource>(IQueryable<TSource>)
Returns the first element of a sequence, or a default value if the sequence contains no elements.
D eclaration
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
Parameters
source IQueryable<TSource>
The IQueryable<T> to return the first element of.
Returns
TSource
default ( TSource ) if source is empty; otherwise, the first element in source .
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is
found.
D eclaration
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if source is empty or if no element passes the test specified by predicate ; otherwise, the first element in
source that passes the test specified by predicate .
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. Keys are compared by using a specified comparer and the elements of each group are
projected by using a specified function.
D eclaration
D eclaration
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector,
System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TR
esult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector Expression<Func<TKey,IEnumerable<TElement>,TResult>>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a
group and its key.
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. The elements of each group are projected by using a specified function.
D eclaration
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector,
System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TR
esult>> resultSelector);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector Expression<Func<TKey,IEnumerable<TElement>,TResult>>
A function to create a result value from each group.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a
group and its key.
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>)
Groups the elements of a sequence according to a specified key selector function and projects the elements for
each group by using a specified function.
D eclaration
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector,
System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
Returns
IQueryable<IGrouping<TKey,TElement>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> contains a sequence of objects of type TElement and a key.
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)
Groups the elements of a sequence and projects the elements for each group by using a specified function. Key
values are compared by using a specified comparer.
D eclaration
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TElement>>
GroupBy<TSource,TKey,TElement> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector,
System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<IGrouping<TKey,TElement>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> contains a sequence of objects of type TElement and a key.
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key.
D eclaration
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TResult> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector,
System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TRe
sult>> resultSelector);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
resultSelector Expression<Func<TKey,IEnumerable<TSource>,TResult>>
A function to create a result value from each group.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a
group and its key.
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from
each group and its key. Keys are compared by using a specified comparer.
D eclaration
D eclaration
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TResult> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector,
System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TRe
sult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
resultSelector Expression<Func<TKey,IEnumerable<TSource>,TResult>>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a
group and its key.
Groups the elements of a sequence according to a specified key selector function and compares the keys by using
a specified comparer.
D eclaration
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey>
(this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<IGrouping<TKey,TSource>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> contains a sequence of objects and a key.
GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
Returns
IQueryable<IGrouping<TKey,TSource>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a sequence of objects and a key.
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>)
Correlates the elements of two sequences based on key equality and groups the results. The default equality
comparer is used to compare keys.
D eclaration
public static System.Linq.IQueryable<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
System.Linq.Expressions.Expression<Func<TOuter,TKey>> outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey>> innerKeySelector,
System.Linq.Expressions.Expression<Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TR
esult>> resultSelector);
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,IEnumerable<TInner>,TResult>>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
Returns
IQueryable<TResult>
An IQueryable<T> that contains elements of type TResult obtained by performing a grouped join on two sequences.
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>, IEqualityComparer<TKey>)
Correlates the elements of two sequences based on key equality and groups the results. A specified
IEqualityComparer<T> is used to compare keys.
D eclaration
public static System.Linq.IQueryable<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
System.Linq.Expressions.Expression<Func<TOuter,TKey>> outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey>> innerKeySelector,
System.Linq.Expressions.Expression<Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TR
esult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,IEnumerable<TInner>,TResult>>
A function to create a result element from an element from the first sequence and a collection of matching elements from the second
sequence.
comparer IEqualityComparer<TKey>
A comparer to hash and compare keys.
Returns
IQueryable<TResult>
An IQueryable<T> that contains elements of type TResult obtained by performing a grouped join on two sequences.
Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Produces the set intersection of two sequences by using the default equality comparer to compare values.
D eclaration
public static System.Linq.IQueryable<TSource> Intersect<TSource> (this
System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource>
source2);
Parameters
source1 IQueryable<TSource>
A sequence whose distinct elements that also appear in source2 are returned.
source2 IEnumerable<TSource>
A sequence whose distinct elements that also appear in the first sequence are returned.
Returns
IQueryable<TSource>
A sequence that contains the set intersection of the two sequences.
Produces the set intersection of two sequences by using the specified IEqualityComparer<T> to compare values.
D eclaration
public static System.Linq.IQueryable<TSource> Intersect<TSource> (this
System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource>
source2, System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose distinct elements that also appear in source2 are returned.
source2 IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in the first sequence are returned.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the set intersection of the two sequences.
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>, Expression<Func<TOuter,TInner,TResult>>)
Correlates the elements of two sequences based on matching keys. The default equality comparer is used to
compare keys.
D eclaration
public static System.Linq.IQueryable<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
System.Linq.Expressions.Expression<Func<TOuter,TKey>> outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey>> innerKeySelector,
System.Linq.Expressions.Expression<Func<TOuter,TInner,TResult>> resultSelector);
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,TInner,TResult>>
A function to create a result element from two matching elements.
Returns
IQueryable<TResult>
An IQueryable<T> that has elements of type TResult obtained by performing an inner join on two sequences.
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>, Expression<Func<TOuter,TInner,TResult>>,
IEqualityComparer<TKey>)
Correlates the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to
compare keys.
D eclaration
public static System.Linq.IQueryable<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
System.Linq.Expressions.Expression<Func<TOuter,TKey>> outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey>> innerKeySelector,
System.Linq.Expressions.Expression<Func<TOuter,TInner,TResult>> resultSelector,
System.Collections.Generic.IEqualityComparer<TKey> comparer);
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,TInner,TResult>>
A function to create a result element from two matching elements.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
IQueryable<TResult>
An IQueryable<T> that has elements of type TResult obtained by performing an inner join on two sequences.
Last<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the last element of.
Returns
TSource
The value at the last position in source .
Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
The last element in source that passes the test specified by predicate .
LastOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
D eclaration
public static TSource LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if source is empty or if no elements pass the test in the predicate function; otherwise, the last element of
source that passes the test in the predicate function.
LastOrDefault<TSource>(IQueryable<TSource>)
Returns the last element in a sequence, or a default value if the sequence contains no elements.
D eclaration
public static TSource LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the last element of.
Returns
TSource
default ( TSource ) if source is empty; otherwise, the last element in source .
LongCount<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
Returns
Int64
The number of elements in source .
LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Returns an Int64 that represents the number of elements in a sequence that satisfy a condition.
D eclaration
public static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Int64
The number of elements in source that satisfy the condition in the predicate function.
Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)
Invokes a projection function on each element of a generic IQueryable<T> and returns the maximum resulting
value.
D eclaration
public static TResult Max<TSource,TResult> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to determine the maximum of.
selector Expression<Func<TSource,TResult>>
A projection function to apply to each element.
Returns
TResult
The maximum value in the sequence.
Max<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
A sequence of values to determine the maximum of.
Returns
TSource
The maximum value in the sequence.
Min<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)
Invokes a projection function on each element of a generic IQueryable<T> and returns the minimum resulting
value.
D eclaration
public static TResult Min<TSource,TResult> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to determine the minimum of.
selector Expression<Func<TSource,TResult>>
A projection function to apply to each element.
Returns
TResult
The minimum value in the sequence.
Min<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
A sequence of values to determine the minimum of.
Returns
TSource
The minimum value in the sequence.
OfType<TResult>(IQueryable)
Parameters
source IQueryable
An IQueryable whose elements to filter.
Returns
IQueryable<TResult>
A collection that contains the elements from source that have type TResult .
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)
Parameters
source IQueryable<TSource>
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
OrderByDescending<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted in descending order according to a key.
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted in descending order according to a key.
Prepend<TSource>(IQueryable<TSource>, TSource)
D eclaration
public static System.Linq.IQueryable<TSource> Prepend<TSource> (this
System.Linq.IQueryable<TSource> source, TSource element);
Parameters
source IQueryable<TSource>
element TSource
Returns
IQueryable<TSource>
Reverse<TSource>(IQueryable<TSource>)
Parameters
source IQueryable<TSource>
A sequence of values to reverse.
Returns
IQueryable<TSource>
An IQueryable<T> whose elements correspond to those of the input sequence in reverse order.
Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>)
Projects each element of a sequence into a new form by incorporating the element's index.
D eclaration
public static System.Linq.IQueryable<TResult> Select<TSource,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,TResult>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,Int32,TResult>>
selector Expression<Func<TSource,Int32,TResult>>
A projection function to apply to each element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a projection function on each element of source .
Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>)
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,TResult>>
A projection function to apply to each element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a projection function on each element of source .
SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TCollection>>>,
Expression<Func<TSource,TCollection,TResult>>)
Projects each element of a sequence to an IEnumerable<T> that incorporates the index of the source element that
produced it. A result selector function is invoked on each element of each intermediate sequence, and the resulting
values are combined into a single, one-dimensional sequence and returned.
D eclaration
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TCollection,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,System.Collections.Generic.IEnumerable<TColl
ection>>> collectionSelector,
System.Linq.Expressions.Expression<Func<TSource,TCollection,TResult>> resultSelector);
Parameters
source IQueryable<TSource>
A sequence of values to project.
collectionSelector Expression<Func<TSource,Int32,IEnumerable<TCollection>>>
A projection function to apply to each element of the input sequence; the second parameter of this function represents the index of the
source element.
resultSelector Expression<Func<TSource,TCollection,TResult>>
resultSelector Expression<Func<TSource,TCollection,TResult>>
A projection function to apply to each element of each intermediate sequence.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking the one-to-many projection function collectionSelector on each
element of source and then mapping each of those sequence elements and their corresponding source element to a result element.
SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection,TResult>>)
Projects each element of a sequence to an IEnumerable<T> and invokes a result selector function on each element
therein. The resulting values from each intermediate sequence are combined into a single, one-dimensional
sequence and returned.
D eclaration
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TCollection,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,System.Collections.Generic.IEnumerable<TCollecti
on>>> collectionSelector, System.Linq.Expressions.Expression<Func<TSource,TCollection,TResult>>
resultSelector);
Parameters
source IQueryable<TSource>
A sequence of values to project.
collectionSelector Expression<Func<TSource,IEnumerable<TCollection>>>
A projection function to apply to each element of the input sequence.
resultSelector Expression<Func<TSource,TCollection,TResult>>
A projection function to apply to each element of each intermediate sequence.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking the one-to-many projection function collectionSelector on each
element of source and then mapping each of those sequence elements and their corresponding source element to a result element.
SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>)
Projects each element of a sequence to an IEnumerable<T> and combines the resulting sequences into one
sequence. The index of each source element is used in the projected form of that element.
D eclaration
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,System.Collections.Generic.IEnumerable<TResu
lt>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,Int32,IEnumerable<TResult>>>
A projection function to apply to each element; the second parameter of this function represents the index of the source element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a one-to-many projection function on each element of the input sequence.
SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>)
Projects each element of a sequence to an IEnumerable<T> and combines the resulting sequences into one
sequence.
D eclaration
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,System.Collections.Generic.IEnumerable<TResult>>
> selector);
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,IEnumerable<TResult>>>
A projection function to apply to each element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a one-to-many projection function on each element of the input sequence.
SequenceEqual<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Determines whether two sequences are equal by using the default equality comparer to compare elements.
D eclaration
public static bool SequenceEqual<TSource> (this System.Linq.IQueryable<TSource> source1,
System.Collections.Generic.IEnumerable<TSource> source2);
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements to compare to those of source2 .
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements to compare to those of the first sequence.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements compare equal; otherwise, false .
Determines whether two sequences are equal by using a specified IEqualityComparer<T> to compare elements.
D eclaration
public static bool SequenceEqual<TSource> (this System.Linq.IQueryable<TSource> source1,
System.Collections.Generic.IEnumerable<TSource> source2,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements to compare to those of source2 .
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements to compare to those of the first sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to use to compare elements.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements compare equal; otherwise, false .
Single<TSource>(IQueryable<TSource>)
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the
sequence.
D eclaration
public static TSource Single<TSource> (this System.Linq.IQueryable<TSource> source);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the single element of.
Returns
TSource
The single element of the input sequence.
Single<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
one such element exists.
D eclaration
public static TSource Single<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return a single element from.
predicate Expression<Func<TSource,Boolean>>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition in predicate .
SingleOrDefault<TSource>(IQueryable<TSource>)
Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an
exception if there is more than one element in the sequence.
D eclaration
public static TSource SingleOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the single element of.
Returns
TSource
The single element of the input sequence, or default ( TSource ) if the sequence contains no elements.
SingleOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element
exists; this method throws an exception if more than one element satisfies the condition.
D eclaration
public static TSource SingleOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return a single element from.
predicate Expression<Func<TSource,Boolean>>
A function to test an element for a condition.
Returns
TSource
TSource
The single element of the input sequence that satisfies the condition in predicate , or default ( TSource ) if no such element is found.
Skip<TSource>(IQueryable<TSource>, Int32)
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
D eclaration
public static System.Linq.IQueryable<TSource> Skip<TSource> (this
System.Linq.IQueryable<TSource> source, int count);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return elements from.
count Int32
The number of elements to skip before returning the remaining elements.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements that occur after the specified index in the input sequence.
SkipLast<TSource>(IQueryable<TSource>, Int32)
D eclaration
public static System.Linq.IQueryable<TSource> SkipLast<TSource> (this
System.Linq.IQueryable<TSource> source, int count);
Parameters
source IQueryable<TSource>
count Int32
Returns
IQueryable<TSource>
SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
D eclaration
public static System.Linq.IQueryable<TSource> SkipWhile<TSource> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>>
predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return elements from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from source starting at the first element in the linear series that does not pass the test
specified by predicate .
SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
The element's index is used in the logic of the predicate function.
D eclaration
public static System.Linq.IQueryable<TSource> SkipWhile<TSource> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to return elements from.
predicate Expression<Func<TSource,Int32,Boolean>>
A function to test each element for a condition; the second parameter of this function represents the index of the source element.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from source starting at the first element in the linear series that does not pass the test
specified by predicate .
Sum(IQueryable<Single>)
Parameters
source IQueryable<Single>
A sequence of Single values to calculate the sum of.
Returns
Single
The sum of the values in the sequence.
Sum(IQueryable<Nullable<Single>>)
Parameters
source IQueryable<Nullable<Single>>
A sequence of nullable Single values to calculate the sum of.
Returns
Nullable<Single>
The sum of the values in the sequence.
Sum(IQueryable<Nullable<Int64>>)
Parameters
source IQueryable<Nullable<Int64>>
A sequence of nullable Int64 values to calculate the sum of.
Returns
Nullable<Int64>
The sum of the values in the sequence.
Sum(IQueryable<Nullable<Int32>>)
Parameters
source IQueryable<Nullable<Int32>>
A sequence of nullable Int32 values to calculate the sum of.
Returns
Nullable<Int32>
The sum of the values in the sequence.
Sum(IQueryable<Nullable<Double>>)
Parameters
source IQueryable<Nullable<Double>>
A sequence of nullable Double values to calculate the sum of.
Returns
Nullable<Double>
The sum of the values in the sequence.
Sum(IQueryable<Double>)
Parameters
source IQueryable<Double>
A sequence of Double values to calculate the sum of.
Returns
Double
The sum of the values in the sequence.
Sum(IQueryable<Int64>)
Parameters
source IQueryable<Int64>
A sequence of Int64 values to calculate the sum of.
Returns
Int64
The sum of the values in the sequence.
Sum(IQueryable<Int32>)
Parameters
source IQueryable<Int32>
A sequence of Int32 values to calculate the sum of.
Returns
Int32
The sum of the values in the sequence.
Sum(IQueryable<Nullable<Decimal>>)
Parameters
source IQueryable<Nullable<Decimal>>
A sequence of nullable Decimal values to calculate the sum of.
Returns
Nullable<Decimal>
The sum of the values in the sequence.
Sum(IQueryable<Decimal>)
Parameters
source IQueryable<Decimal>
A sequence of Decimal values to calculate the sum of.
Returns
Decimal
The sum of the values in the sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>)
Computes the sum of the sequence of nullable Single values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<float> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<float>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Single>>>
A projection function to apply to each element.
Returns
Nullable<Single>
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>)
Computes the sum of the sequence of nullable Int64 values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<long> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<long>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Int64>>>
A projection function to apply to each element.
Returns
Nullable<Int64>
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>)
Computes the sum of the sequence of nullable Int32 values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<int> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<int>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Int32>>>
A projection function to apply to each element.
Returns
Nullable<Int32>
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>)
Computes the sum of the sequence of nullable Double values that is obtained by invoking a projection function on
each element of the input sequence.
D eclaration
public static Nullable<double> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<double>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Double>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>)
Computes the sum of the sequence of Single values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static float Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,float>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Single>>
A projection function to apply to each element.
Returns
Single
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>)
Computes the sum of the sequence of Int64 values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static long Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,long>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Int64>>
A projection function to apply to each element.
Returns
Int64
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>)
Computes the sum of the sequence of Int32 values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static int Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Int32>>
A projection function to apply to each element.
Returns
Int32
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>)
Computes the sum of the sequence of Double values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static double Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,double>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Double>>
A projection function to apply to each element.
Returns
Double
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>)
Computes the sum of the sequence of Decimal values that is obtained by invoking a projection function on each
element of the input sequence.
D eclaration
public static decimal Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,decimal>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Decimal>>
A projection function to apply to each element.
Returns
Decimal
The sum of the projected values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>)
Computes the sum of the sequence of nullable Decimal values that is obtained by invoking a projection function
on each element of the input sequence.
D eclaration
public static Nullable<decimal> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<decimal>>> selector);
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Decimal>>>
A projection function to apply to each element.
Returns
Nullable<Decimal>
The sum of the projected values.
Take<TSource>(IQueryable<TSource>, Int32)
Parameters
source IQueryable<TSource>
The sequence to return elements from.
count Int32
The number of elements to return.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the specified number of elements from the start of source .
TakeLast<TSource>(IQueryable<TSource>, Int32)
D eclaration
public static System.Linq.IQueryable<TSource> TakeLast<TSource> (this
System.Linq.IQueryable<TSource> source, int count);
Parameters
source IQueryable<TSource>
count Int32
Returns
IQueryable<TSource>
TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)
Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic
of the predicate function.
D eclaration
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
Parameters
source IQueryable<TSource>
The sequence to return elements from.
predicate Expression<Func<TSource,Int32,Boolean>>
A function to test each element for a condition; the second parameter of the function represents the index of the element in the source
sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence occurring before the element at which the test specified by
predicate no longer passes.
TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Parameters
source IQueryable<TSource>
The sequence to return elements from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence occurring before the element at which the test specified by
predicate no longer passes.
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)
Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
D eclaration
public static System.Linq.IOrderedQueryable<TSource> ThenBy<TSource,TKey> (this
System.Linq.IOrderedQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
D eclaration
public static System.Linq.IOrderedQueryable<TSource> ThenBy<TSource,TKey> (this
System.Linq.IOrderedQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)
Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
D eclaration
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.IOrderedQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted in descending order according to a key.
ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>,
IComparer<TKey>)
Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
D eclaration
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.IOrderedQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector,
System.Collections.Generic.IComparer<TKey> comparer);
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
A collection whose elements are sorted in descending order according to a key.
Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Produces the set union of two sequences by using the default equality comparer.
D eclaration
public static System.Linq.IQueryable<TSource> Union<TSource> (this
System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource>
source2);
Parameters
source1 IQueryable<TSource>
A sequence whose distinct elements form the first set for the union operation.
source2 IEnumerable<TSource>
A sequence whose distinct elements form the second set for the union operation.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the elements from both input sequences, excluding duplicates.
Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)
Parameters
source1 IQueryable<TSource>
A sequence whose distinct elements form the first set for the union operation.
source2 IEnumerable<TSource>
A sequence whose distinct elements form the second set for the union operation.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the elements from both input sequences, excluding duplicates.
Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)
Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate
function.
D eclaration
public static System.Linq.IQueryable<TSource> Where<TSource> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
Parameters
source IQueryable<TSource>
An IQueryable<T> to filter.
predicate Expression<Func<TSource,Int32,Boolean>>
A function to test each element for a condition; the second parameter of the function represents the index of the element in the source
sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence that satisfy the condition specified by predicate .
Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
Parameters
source IQueryable<TSource>
An IQueryable<T> to filter.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence that satisfy the condition specified by predicate .
Zip<TFirst,TSecond,TResult>(IQueryable<TFirst>, IEnumerable<TSecond>,
Expression<Func<TFirst,TSecond,TResult>>)
Parameters
source1 IQueryable<TFirst>
The first sequence to merge.
source2 IEnumerable<TSecond>
The second sequence to merge.
resultSelector Expression<Func<TFirst,TSecond,TResult>>
A function that specifies how to merge the elements from the two sequences.
Returns
IQueryable<TResult>
An IQueryable<T> that contains merged elements of two input sequences.
Queryable.Aggregate
I n this Article
Overloads
Aggregate<TSource,TAccumulate,TResult>(IQueryable<
TSource>, TAccumulate, Expression<Func<TAccumulate, Applies an accumulator function over a sequence. The
TSource,TAccumulate>>, Expression<Func<TAccumulate, specified seed value is used as the initial accumulator value,
TResult>>) and the specified function is used to select the result value.
Aggregate<TSource,TAccumulate>(IQueryable<TSource>,
TAccumulate, Expression<Func<TAccumulate,TSource, Applies an accumulator function over a sequence. The
TAccumulate>>) specified seed value is used as the initial accumulator value.
Aggregate<TSource>(IQueryable<TSource>, Expression<
Func<TSource,TSource,TSource>>) Applies an accumulator function over a sequence.
Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>,
TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>,
Expression<Func<TAccumulate,TResult>>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and
the specified function is used to select the result value.
public static TResult Aggregate<TSource,TAccumulate,TResult> (this System.Linq.IQueryable<TSource>
source, TAccumulate seed, System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>>
func, System.Linq.Expressions.Expression<Func<TAccumulate,TResult>> selector);
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
TResult
The type of the resulting value.
Parameters
source IQueryable<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Expression<Func<TAccumulate,TSource,TAccumulate>>
An accumulator function to invoke on each element.
selector Expression<Func<TAccumulate,TResult>>
A function to transform the final accumulator value into the result value.
Returns
TResult
The transformed final accumulator value.
Exceptions
ArgumentNullException
source or func or selector is null .
Examples
The following code example demonstrates how to use Aggregate<TSource,TAccumulate,TResult>
(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>,
Expression<Func<TAccumulate,TResult>>) to apply an accumulator function and a result selector.
string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
Console.WriteLine(
"The fruit with the longest name is {0}.",
longestName);
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) method
generates a MethodCallExpression that represents calling Aggregate<TSource,TAccumulate,TResult>
(IQueryable<TSource>, TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>,
Expression<Func<TAccumulate,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Aggregate<TSource,TAccumulate,TResult>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>, Expression<Func<TAccumulate,TResult>>) depends on the
implementation of the type of the source parameter. The expected behavior is that the specified function, func , is
applied to each value in the source sequence and the accumulated value is returned. The seed parameter is used as the
seed value for the accumulated value, which corresponds to the first parameter in func . The final accumulated value is
passed to selector to obtain the result value.
To simplify common aggregation operations, the set of standard query operators also includes two counting methods,
Count and LongCount, and four numeric aggregation methods, namely Max, Min, Sum, and Average.
Aggregate<TSource,TAccumulate>(IQueryable<TSource>,
TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>)
Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
public static TAccumulate Aggregate<TSource,TAccumulate> (this System.Linq.IQueryable<TSource>
source, TAccumulate seed, System.Linq.Expressions.Expression<Func<TAccumulate,TSource,TAccumulate>>
func);
Type Parameters
TSource
The type of the elements of source .
TAccumulate
The type of the accumulator value.
Parameters
source IQueryable<TSource>
A sequence to aggregate over.
seed TAccumulate
The initial accumulator value.
func Expression<Func<TAccumulate,TSource,TAccumulate>>
An accumulator function to invoke on each element.
Returns
TAccumulate
The final accumulator value.
Exceptions
ArgumentNullException
source or func is null .
Examples
The following code example demonstrates how to use Aggregate<TSource,TAccumulate>(IQueryable<TSource>,
TAccumulate, Expression<Func<TAccumulate,TSource,TAccumulate>>) to apply an accumulator function when a seed
value is provided to the function.
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>) method generates a MethodCallExpression that represents
calling Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Aggregate<TSource,TAccumulate>(IQueryable<TSource>, TAccumulate,
Expression<Func<TAccumulate,TSource,TAccumulate>>) depends on the implementation of the type of the source
parameter. The expected behavior is that the specified function, func , is applied to each value in the source sequence
and the accumulated value is returned. The seed parameter is used as the seed value for the accumulated value, which
corresponds to the first parameter in func .
To simplify common aggregation operations, the set of standard query operators also includes two counting methods,
Count and LongCount, and four numeric aggregation methods, namely Max, Min, Sum, and Average.
Aggregate<TSource>(IQueryable<TSource>,
Expression<Func<TSource,TSource,TSource>>)
Applies an accumulator function over a sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence to aggregate over.
func Expression<Func<TSource,TSource,TSource>>
An accumulator function to apply to each element.
Returns
TSource
The final accumulator value.
Exceptions
ArgumentNullException
source or func is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Aggregate<TSource>(IQueryable<TSource>,
Expression<Func<TSource,TSource,TSource>>) to build a sentence from an array of strings.
string sentence = "the quick brown fox jumps over the lazy dog";
Console.WriteLine(reversed);
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Aggregate<TSource>(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) method generates a
MethodCallExpression that represents calling Aggregate<TSource>(IQueryable<TSource>,
Expression<Func<TSource,TSource,TSource>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Aggregate<TSource>
(IQueryable<TSource>, Expression<Func<TSource,TSource,TSource>>) depends on the implementation of the type of
the source parameter. The expected behavior is that the specified function, func , is applied to each value in the source
sequence and the accumulated value is returned. The first value in source is used as the seed value for the
accumulated value, which corresponds to the first parameter in func .
To simplify common aggregation operations, the set of standard query operators also includes two counting methods,
Count and LongCount, and four numeric aggregation methods, namely Max, Min, Sum, and Average.
Queryable.All
I n this Article
Determines whether all the elements of a sequence satisfy a condition.
public static bool All<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence whose elements to test for a condition.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Boolean
true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty;
otherwise, false .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use All<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to determine whether all the elements in a sequence satisfy a condition.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
// Determine whether all pet names in the array start with 'B'.
bool allStartWithB =
pets.AsQueryable().All(pet => pet.Name.StartsWith("B"));
Console.WriteLine(
"{0} pet names start with 'B'.",
allStartWithB ? "All" : "Not all");
}
// Determine which people have pets that are all older than 5.
IEnumerable<string> names = from person in people
where person.Pets.AsQueryable().All(pet => pet.Age > 5)
select person.LastName;
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling All<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling All<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the source
parameter's type. The expected behavior is that it determines if all the elements in source satisfy the condition in
predicate .
Queryable.Any
I n this Article
Overloads
Any<TSource>(IQueryable<TSource>)
Determines whether a sequence contains any elements.
Any<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Boolean>>) Determines whether any element of a sequence satisfies a
condition.
Any<TSource>(IQueryable<TSource>)
Determines whether a sequence contains any elements.
public static bool Any<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence to check for being empty.
Returns
Boolean
true if the source sequence contains any elements; otherwise, false .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Any<TSource>(IQueryable<TSource>) to determine whether a
sequence contains any elements.
List<int> numbers = new List<int> { 1, 2 };
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
class Person
{
public string LastName { get; set; }
public Pet[] Pets { get; set; }
}
Haas
Fakhouri
Philips
*/
}
Remarks
The Any<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Any<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Any<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it determines if source contains any elements.
Any<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Determines whether any element of a sequence satisfies a condition.
Determines whether any element of a sequence satisfies a condition.
public static bool Any<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence whose elements to test for a condition.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Boolean
true if any elements in the source sequence pass the test in the specified predicate; otherwise, false .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use Any<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to determine whether any element in a sequence satisfies a condition.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
public bool Vaccinated { get; set; }
}
Console.WriteLine(
"There {0} unvaccinated animals over age one.",
unvaccinated ? "are" : "are not any");
}
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Any<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling Any<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Any<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it determines if any of the elements of source satisfy the condition
specified by predicate .
Queryable.Append
I n this Article
public static System.Linq.IQueryable<TSource> Append<TSource> (this System.Linq.IQueryable<TSource>
source, TSource element);
Type Parameters
TSource
Parameters
source IQueryable<TSource>
element TSource
Returns
IQueryable<TSource>
Queryable.AsQueryable
I n this Article
Overloads
AsQueryable(IEnumerable)
Converts an IEnumerable to an IQueryable.
AsQueryable<TElement>(IEnumerable<TElement>)
Converts a generic IEnumerable<T> to a generic
IQueryable<T>.
AsQueryable(IEnumerable)
Converts an IEnumerable to an IQueryable.
public static System.Linq.IQueryable AsQueryable (this System.Collections.IEnumerable source);
Parameters
source IEnumerable
A sequence to convert.
Returns
IQueryable
An IQueryable that represents the input sequence.
Exceptions
ArgumentException
source does not implement IEnumerable<T> for some T .
ArgumentNullException
source is null .
Remarks
If the type of source implements IQueryable<T>, AsQueryable(IEnumerable) returns it directly. Otherwise, it returns
an IQueryable<T> that executes queries by calling the equivalent query operator methods in Enumerable instead of
those in Queryable.
This method assumes that source implements IEnumerable<T> for some T . At runtime, the result is of type
IQueryable<T> for the same T . This method is useful in dynamic scenarios when you do not statically know the type of
T.
AsQueryable<TElement>(IEnumerable<TElement>)
Converts a generic IEnumerable<T> to a generic IQueryable<T>.
/*
This code produces the following output:
Remarks
If the type of source implements IQueryable<T>, AsQueryable<TElement>(IEnumerable<TElement>) returns it
directly. Otherwise, it returns an IQueryable<T> that executes queries by calling the equivalent query operator methods
in Enumerable instead of those in Queryable.
Queryable.Average
I n this Article
Overloads
Average(IQueryable<Single>)
Computes the average of a sequence of Single values.
Average(IQueryable<Nullable<Single>>)
Computes the average of a sequence of nullable Single values.
Average(IQueryable<Nullable<Int64>>)
Computes the average of a sequence of nullable Int64 values.
Average(IQueryable<Nullable<Double>>)
Computes the average of a sequence of nullable Double
values.
Average(IQueryable<Nullable<Int32>>)
Computes the average of a sequence of nullable Int32 values.
Average(IQueryable<Int64>)
Computes the average of a sequence of Int64 values.
Average(IQueryable<Int32>)
Computes the average of a sequence of Int32 values.
Average(IQueryable<Double>)
Computes the average of a sequence of Double values.
Average(IQueryable<Decimal>)
Computes the average of a sequence of Decimal values.
Average(IQueryable<Nullable<Decimal>>)
Computes the average of a sequence of nullable Decimal
values.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Single>>) Computes the average of a sequence of Single values that is
obtained by invoking a projection function on each element of
the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Single>>>) Computes the average of a sequence of nullable Single values
that is obtained by invoking a projection function on each
element of the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Int64>>>) Computes the average of a sequence of nullable Int64 values
that is obtained by invoking a projection function on each
element of the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Int32>>>) Computes the average of a sequence of nullable Int32 values
that is obtained by invoking a projection function on each
element of the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Double>>>) Computes the average of a sequence of nullable Double
values that is obtained by invoking a projection function on
each element of the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Int64>>) Computes the average of a sequence of Int64 values that is
obtained by invoking a projection function on each element of
the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Int32>>) Computes the average of a sequence of Int32 values that is
obtained by invoking a projection function on each element of
the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Double>>) Computes the average of a sequence of Double values that is
obtained by invoking a projection function on each element of
the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Decimal>>) Computes the average of a sequence of Decimal values that is
obtained by invoking a projection function on each element of
the input sequence.
Average<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Decimal>>>) Computes the average of a sequence of nullable Decimal
values that is obtained by invoking a projection function on
each element of the input sequence.
Average(IQueryable<Single>)
Computes the average of a sequence of Single values.
public static float Average (this System.Linq.IQueryable<float> source);
Parameters
source IQueryable<Single>
A sequence of Single values to calculate the average of.
Returns
Single
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IQueryable<Int32>) to calculate the average of a
sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
Remarks
The Average(IQueryable<Single>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Single>) itself. It then passes the MethodCallExpression to the Execute<TResult>(Expression)
method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Single>) depends on the implementation of the type of the source parameter. The expected
behavior is that it calculates the average of the values in source .
Average(IQueryable<Nullable<Single>>)
Computes the average of a sequence of nullable Single values.
Parameters
source IQueryable<Nullable<Single>>
A sequence of nullable Single values to calculate the average of.
Returns
Nullable<Single>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IQueryable<Nullable<Int64>>) to calculate the average
of a sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
Remarks
The Average(IQueryable<Nullable<Single>>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Nullable<Single>>) itself. It then passes the MethodCallExpression to the Execute<TResult>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Nullable<Single>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it calculates the average of the values in source .
Average(IQueryable<Nullable<Int64>>)
Computes the average of a sequence of nullable Int64 values.
public static Nullable<double> Average (this System.Linq.IQueryable<Nullable<long>> source);
Parameters
source IQueryable<Nullable<Int64>>
A sequence of nullable Int64 values to calculate the average of.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IQueryable<Nullable<Int64>>) to calculate the average
of a sequence of values.
Remarks
The Average(IQueryable<Nullable<Int64>>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Nullable<Int64>>) itself. It then passes the MethodCallExpression to the Execute<TResult>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Nullable<Int64>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it calculates the average of the values in source .
Average(IQueryable<Nullable<Double>>)
Computes the average of a sequence of nullable Double values.
Parameters
source IQueryable<Nullable<Double>>
A sequence of nullable Double values to calculate the average of.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IQueryable<Nullable<Int64>>) to calculate the average
of a sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
long?[] longs = { null, 10007L, 37L, 399846234235L };
Remarks
The Average(IQueryable<Nullable<Double>>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Nullable<Double>>) itself. It then passes the MethodCallExpression to the Execute<TResult>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Nullable<Double>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it calculates the average of the values in source .
Average(IQueryable<Nullable<Int32>>)
Computes the average of a sequence of nullable Int32 values.
Parameters
source IQueryable<Nullable<Int32>>
A sequence of nullable Int32 values to calculate the average of.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IQueryable<Nullable<Int64>>) to calculate the average
of a sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
long?[] longs = { null, 10007L, 37L, 399846234235L };
Remarks
The Average(IQueryable<Nullable<Int32>>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Nullable<Int32>>) itself. It then passes the MethodCallExpression to the Execute<TResult>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Nullable<Int32>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it calculates the average of the values in source .
Average(IQueryable<Int64>)
Computes the average of a sequence of Int64 values.
Parameters
source IQueryable<Int64>
A sequence of Int64 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IQueryable<Int32>) to calculate the average of a
sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
Remarks
The Average(IQueryable<Int64>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Int64>) itself. It then passes the MethodCallExpression to the Execute<TResult>(Expression)
method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Int64>) depends on the implementation of the type of the source parameter. The expected
behavior is that it calculates the average of the values in source .
Average(IQueryable<Int32>)
Computes the average of a sequence of Int32 values.
Parameters
source IQueryable<Int32>
A sequence of Int32 values to calculate the average of.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IQueryable<Int32>) to calculate the average of a
sequence of values.
List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
Remarks
The Average(IQueryable<Int32>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Int32>) itself. It then passes the MethodCallExpression to the Execute<TResult>(Expression)
method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Int32>) depends on the implementation of the type of the source parameter. The expected
behavior is that it calculates the average of the values in source .
Average(IQueryable<Double>)
Computes the average of a sequence of Double values.
Parameters
source IQueryable<Double>
A sequence of Double values to calculate the average of.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IQueryable<Int32>) to calculate the average of a
sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
Remarks
The Average(IQueryable<Double>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Double>) itself. It then passes the MethodCallExpression to the Execute<TResult>(Expression)
method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Double>) depends on the implementation of the type of the source parameter. The expected
behavior is that it calculates the average of the values in source .
Average(IQueryable<Decimal>)
Computes the average of a sequence of Decimal values.
Parameters
source IQueryable<Decimal>
A sequence of Decimal values to calculate the average of.
Returns
Decimal
The average of the sequence of values.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average(IQueryable<Int32>) to calculate the average of a
sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
Remarks
The Average(IQueryable<Decimal>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Decimal>) itself. It then passes the MethodCallExpression to the Execute<TResult>(Expression)
method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Decimal>) depends on the implementation of the type of the source parameter. The expected
behavior is that it calculates the average of the values in source .
Average(IQueryable<Nullable<Decimal>>)
Computes the average of a sequence of nullable Decimal values.
public static Nullable<decimal> Average (this System.Linq.IQueryable<Nullable<decimal>> source);
Parameters
source IQueryable<Nullable<Decimal>>
A sequence of nullable Decimal values to calculate the average of.
Returns
Nullable<Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Average(IQueryable<Nullable<Int64>>) to calculate the average
of a sequence of values.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
long?[] longs = { null, 10007L, 37L, 399846234235L };
Remarks
The Average(IQueryable<Nullable<Decimal>>) method generates a MethodCallExpression that represents calling
Average(IQueryable<Nullable<Decimal>>) itself. It then passes the MethodCallExpression to the Execute<TResult>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Average(IQueryable<Nullable<Decimal>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it calculates the average of the values in source .
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Single>>)
Computes the average of a sequence of Single values that is obtained by invoking a projection function on each
element of the input sequence.
public static float Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,float>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Single>>
A projection function to apply to each element.
Returns
Single
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Single>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Single>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Single>>>)
Computes the average of a sequence of nullable Single values that is obtained by invoking a projection function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Single>>>
A projection function to apply to each element.
Returns
Nullable<Single>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Single>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>) depends on the implementation of the type
of the source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int64>>>)
Computes the average of a sequence of nullable Int64 values that is obtained by invoking a projection function on each
element of the input sequence.
public static Nullable<double> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<long>>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Int64>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int64>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>) depends on the implementation of the type of
the source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int32>>>)
Computes the average of a sequence of nullable Int32 values that is obtained by invoking a projection function on each
element of the input sequence.
public static Nullable<double> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<int>>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Int32>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int32>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>) depends on the implementation of the type of
the source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Double>>>)
Computes the average of a sequence of nullable Double values that is obtained by invoking a projection function on
each element of the input sequence.
public static Nullable<double> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<double>>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Double>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Double>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>) depends on the implementation of the type
of the source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int64>>)
Computes the average of a sequence of Int64 values that is obtained by invoking a projection function on each element
of the input sequence.
public static double Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,long>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Int64>>
A projection function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int64>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int64>>) depends on the implementation of the type of the source
parameter. The expected behavior is that it calculates the average of the values in source after invoking selector on
each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>)
Computes the average of a sequence of Int32 values that is obtained by invoking a projection function on each element
of the input sequence.
public static double Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Int32>>
A projection function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int32>>) depends on the implementation of the type of the source
parameter. The expected behavior is that it calculates the average of the values in source after invoking selector on
each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>)
Computes the average of a sequence of Double values that is obtained by invoking a projection function on each
element of the input sequence.
public static double Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,double>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Double>>
A projection function to apply to each element.
Returns
Double
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Double>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Decimal>>)
Computes the average of a sequence of Decimal values that is obtained by invoking a projection function on each
element of the input sequence.
public static decimal Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,decimal>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values that are used to calculate an average.
selector Expression<Func<TSource,Decimal>>
A projection function to apply to each element.
Returns
Decimal
The average of the sequence of values.
Exceptions
ArgumentNullException
source or selector is null .
InvalidOperationException
source contains no elements.
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Decimal>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Decimal>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Decimal>>>)
Computes the average of a sequence of nullable Decimal values that is obtained by invoking a projection function on
each element of the input sequence.
public static Nullable<decimal> Average<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<decimal>>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to calculate the average of.
selector Expression<Func<TSource,Nullable<Decimal>>>
A projection function to apply to each element.
Returns
Nullable<Decimal>
The average of the sequence of values, or null if the source sequence is empty or contains only null values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) to calculate the average String length in a sequence of values of type String.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Average<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>) method generates a
MethodCallExpression that represents calling Average<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Decimal>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Average<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>) depends on the implementation of the type
of the source parameter. The expected behavior is that it calculates the average of the values in source after invoking
selector on each value.
Queryable.Cast
I n this Article
Converts the elements of an IQueryable to the specified type.
public static System.Linq.IQueryable<TResult> Cast<TResult> (this System.Linq.IQueryable source);
Type Parameters
TResult
The type to convert the elements of source to.
Parameters
source IQueryable
The IQueryable that contains the elements to be converted.
Returns
IQueryable<TResult>
An IQueryable<T> that contains each element of the source sequence converted to the specified type.
Exceptions
ArgumentNullException
source is null .
InvalidCastException
An element in the sequence cannot be cast to type TResult .
Examples
The following code example demonstrates how to use Cast<TResult>(IQueryable) to convert objects in a sequence to
type String.
g
b
v
*/
Remarks
The Cast<TResult>(IQueryable) method generates a MethodCallExpression that represents calling Cast<TResult>
(IQueryable) itself as a constructed generic method. It then passes the MethodCallExpression to the
CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Cast<TResult>
(IQueryable) depends on the implementation of the type of the source parameter. The expected behavior is that it
converts the values in source to type TResult .
Queryable.Concat
I n this Article
Concatenates two sequences.
public static System.Linq.IQueryable<TSource> Concat<TSource> (this System.Linq.IQueryable<TSource>
source1, System.Collections.Generic.IEnumerable<TSource> source2);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
The first sequence to concatenate.
source2 IEnumerable<TSource>
The sequence to concatenate to the first sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the concatenated elements of the two input sequences.
Exceptions
ArgumentNullException
source1 or source2 is null .
Examples
The following code example demonstrates how to use Concat<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) to concatenate two sequences.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Remarks
The Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>) method generates a MethodCallExpression that
represents calling Concat<TSource>(IQueryable<TSource>, IEnumerable<TSource>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Concat<TSource>
(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type of the source1
parameter. The expected behavior is that the elements in source2 are concatenated to those of source1 to create a
new sequence.
Queryable.Contains
I n this Article
Overloads
Contains<TSource>(IQueryable<TSource>, TSource, IEquality
Comparer<TSource>) Determines whether a sequence contains a specified element
by using a specified IEqualityComparer<T>.
Contains<TSource>(IQueryable<TSource>, TSource)
Determines whether a sequence contains a specified element
by using the default equality comparer.
Contains<TSource>(IQueryable<TSource>, TSource,
IEqualityComparer<TSource>)
Determines whether a sequence contains a specified element by using a specified IEqualityComparer<T>.
public static bool Contains<TSource> (this System.Linq.IQueryable<TSource> source, TSource item,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> in which to locate item .
item TSource
The object to locate in the sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
Boolean
true if the input sequence contains an element that has the specified value; otherwise, false .
Exceptions
ArgumentNullException
source is null .
Remarks
The Contains<TSource>(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) method generates a
MethodCallExpression that represents calling Contains<TSource>(IQueryable<TSource>, TSource,
IEqualityComparer<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Contains<TSource>
(IQueryable<TSource>, TSource, IEqualityComparer<TSource>) depends on the implementation of the type of the
source parameter. The expected behavior is that it determines if source contains item by using comparer to
compare values.
Contains<TSource>(IQueryable<TSource>, TSource)
Determines whether a sequence contains a specified element by using the default equality comparer.
public static bool Contains<TSource> (this System.Linq.IQueryable<TSource> source, TSource item);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> in which to locate item .
item TSource
The object to locate in the sequence.
Returns
Boolean
true if the input sequence contains an element that has the specified value; otherwise, false .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Contains<TSource>(IQueryable<TSource>, TSource) to
determine whether a sequence contains a specific element.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
Console.WriteLine(
"The array {0} contain '{1}'.",
hasMango ? "does" : "does not",
mango);
Remarks
The Contains<TSource>(IQueryable<TSource>, TSource) method generates a MethodCallExpression that represents
calling Contains<TSource>(IQueryable<TSource>, TSource) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Contains<TSource>
(IQueryable<TSource>, TSource) depends on the implementation of the type of the source parameter. The expected
behavior is that it determines if source contains item .
Queryable.Count
I n this Article
Overloads
Count<TSource>(IQueryable<TSource>)
Returns the number of elements in a sequence.
Count<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Boolean>>) Returns the number of elements in the specified sequence that
satisfies a condition.
Count<TSource>(IQueryable<TSource>)
Returns the number of elements in a sequence.
public static int Count<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> that contains the elements to be counted.
Returns
Int32
The number of elements in the input sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The number of elements in source is larger than MaxValue.
Examples
The following code example demonstrates how to use Count<TSource>(IQueryable<TSource>) to count the elements
in a sequence.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
Console.WriteLine(
"There are {0} items in the array.",
numberOfFruits);
Remarks
The Count<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Count<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Count<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it counts the number of items in source .
Count<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the number of elements in the specified sequence that satisfies a condition.
public static int Count<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Int32
The number of elements in the sequence that satisfies the condition in the predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
OverflowException
The number of elements in source is larger than MaxValue.
Examples
The following code example demonstrates how to use Count<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to count the elements in a sequence that satisfy a condition.
class Pet
{
public string Name { get; set; }
public bool Vaccinated { get; set; }
}
Console.WriteLine(
"There are {0} unvaccinated animals.",
numberUnvaccinated);
}
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling Count<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Count<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it counts the number of items in source that satisfy the condition
specified by predicate .
Queryable.DefaultIfEmpty
I n this Article
Overloads
DefaultIfEmpty<TSource>(IQueryable<TSource>)
Returns the elements of the specified sequence or the type
parameter's default value in a singleton collection if the
sequence is empty.
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)
Returns the elements of the specified sequence or the
specified value in a singleton collection if the sequence is
empty.
DefaultIfEmpty<TSource>(IQueryable<TSource>)
Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the
sequence is empty.
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource> (this
System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> to return a default value for if empty.
Returns
IQueryable<TSource>
An IQueryable<T> that contains default ( TSource ) if source is empty; otherwise, source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code examples demonstrate how to use DefaultIfEmpty<TSource>(IQueryable<TSource>) to provide a
default value in case the source sequence is empty.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Barley
*/
Remarks
The DefaultIfEmpty<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents
calling DefaultIfEmpty<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
DefaultIfEmpty<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source
parameter. The expected behavior is that it returns source if it is not empty. Otherwise, it returns an IQueryable<T>
that contains default(TSource) .
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)
Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.
public static System.Linq.IQueryable<TSource> DefaultIfEmpty<TSource> (this
System.Linq.IQueryable<TSource> source, TSource defaultValue);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> to return the specified value for if empty.
defaultValue TSource
The value to return if the sequence is empty.
Returns
IQueryable<TSource>
An IQueryable<T> that contains defaultValue if source is empty; otherwise, source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example shows a situation in which it is useful to call DefaultIfEmpty<TSource>
(IQueryable<TSource>, TSource) in a LINQ query. A default value is passed to DefaultIfEmpty<TSource>
(IQueryable<TSource>, TSource) in this example.
// Create a list of Pet objects.
List<Pet> pets =
new List<Pet>{ new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
/*
This code produces the following output:
Remarks
The DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method generates a MethodCallExpression that
represents calling DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) itself as a constructed generic method. It
then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider
represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) depends on the implementation of the type of the source
parameter. The expected behavior is that it returns source if it is not empty. Otherwise, it returns an IQueryable<T>
that contains defaultValue .
Queryable.Distinct
I n this Article
Overloads
Distinct<TSource>(IQueryable<TSource>)
Returns distinct elements from a sequence by using the
default equality comparer to compare values.
Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<
TSource>) Returns distinct elements from a sequence by using a specified
IEqualityComparer<T> to compare values.
Distinct<TSource>(IQueryable<TSource>)
Returns distinct elements from a sequence by using the default equality comparer to compare values.
public static System.Linq.IQueryable<TSource> Distinct<TSource> (this
System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> to remove duplicates from.
Returns
IQueryable<TSource>
An IQueryable<T> that contains distinct elements from source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Distinct<TSource>(IQueryable<TSource>) to return distinct
elements from a sequence.
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
Console.WriteLine("Distinct ages:");
/*
This code produces the following output:
Distinct ages:
21
46
55
17
*/
Remarks
The Distinct<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Distinct<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Distinct<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it returns an unordered sequence of the unique items in source .
Distinct<TSource>(IQueryable<TSource>,
IEqualityComparer<TSource>)
Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.
public static System.Linq.IQueryable<TSource> Distinct<TSource> (this
System.Linq.IQueryable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>
comparer);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> to remove duplicates from.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains distinct elements from source .
Exceptions
ArgumentNullException
source or comparer is null .
Remarks
The Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) method generates a
MethodCallExpression that represents calling Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>)
itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Distinct<TSource>
(IQueryable<TSource>, IEqualityComparer<TSource>) depends on the implementation of the type of the source
parameter. The expected behavior is that it returns an unordered sequence of the unique items in source by using
comparer to compare values.
Queryable.ElementAt
I n this Article
Returns the element at a specified index in a sequence.
public static TSource ElementAt<TSource> (this System.Linq.IQueryable<TSource> source, int index);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
The element at the specified position in source .
Exceptions
ArgumentNullException
source is null .
ArgumentOutOfRangeException
index is less than zero.
Examples
The following code example demonstrates how to use ElementAt<TSource>(IQueryable<TSource>, Int32) to return an
element at a specific position in a sequence.
string[] names = { "Hartono, Tommy", "Adams, Terry",
"Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
string name =
names.AsQueryable().ElementAt(random.Next(0, names.Length));
/*
This code produces the following sample output.
Yours may be different due to the use of Random.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
index Int32
The zero-based index of the element to retrieve.
Returns
TSource
default ( TSource ) if index is outside the bounds of source ; otherwise, the element at the specified position in
source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use ElementAtOrDefault<TSource>(IQueryable<TSource>, Int32).
This example uses a value for index that is outside the bounds of the source sequence.
Console.WriteLine(
"The name chosen at index {0} is '{1}'.",
index,
String.IsNullOrEmpty(name) ? "[NONE AT THIS INDEX]" : name);
/*
This code produces the following output:
Overloads
Except<TSource>(IQueryable<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) Produces the set difference of two sequences by using the
specified IEqualityComparer<T> to compare values.
Except<TSource>(IQueryable<TSource>, IEnumerable<
TSource>) Produces the set difference of two sequences by using the
default equality comparer to compare values.
Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>,
IEqualityComparer<TSource>)
Produces the set difference of two sequences by using the specified IEqualityComparer<T> to compare values.
public static System.Linq.IQueryable<TSource> Except<TSource> (this System.Linq.IQueryable<TSource>
source1, System.Collections.Generic.IEnumerable<TSource> source2,
System.Collections.Generic.IEqualityComparer<TSource> comparer);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements that are not also in source2 will be returned.
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will not appear in the returned sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the set difference of the two sequences.
Exceptions
ArgumentNullException
source1 or source2 is null .
Remarks
The Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method
generates a MethodCallExpression that represents calling Except<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Except<TSource>
(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) depends on the implementation of the
type of the source1 parameter. The expected behavior is that all the elements in source1 are returned except for those
that are also in source2 , and comparer is used to compare values.
Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Produces the set difference of two sequences by using the default equality comparer to compare values.
public static System.Linq.IQueryable<TSource> Except<TSource> (this System.Linq.IQueryable<TSource>
source1, System.Collections.Generic.IEnumerable<TSource> source2);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements that are not also in source2 will be returned.
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements that also occur in the first sequence will not appear in the returned sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the set difference of the two sequences.
Exceptions
ArgumentNullException
source1 or source2 is null .
Examples
The following code example demonstrates how to use Except<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) to return those elements that only appear in the first source sequence.
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.2 };
/*
This code produces the following output:
2
2.1
2.3
2.4
2.5
*/
Remarks
The Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>) method generates a MethodCallExpression that
represents calling Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Except<TSource>
(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type of the source1
parameter. The expected behavior is that all the elements in source1 are returned except for those that are also in
source2 .
Queryable.First
I n this Article
Overloads
First<TSource>(IQueryable<TSource>)
Returns the first element of a sequence.
First<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Boolean>>) Returns the first element of a sequence that satisfies a
specified condition.
First<TSource>(IQueryable<TSource>)
Returns the first element of a sequence.
public static TSource First<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> to return the first element of.
Returns
TSource
The first element in source .
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
The source sequence is empty.
Examples
The following code example demonstrates how to use First<TSource>(IQueryable<TSource>) to return the first
element in a sequence.
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 435, 67, 12, 19 };
Console.WriteLine(first);
/*
This code produces the following output:
9
*/
Remarks
The First<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
First<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling First<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it returns the first element in source .
First<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the first element of a sequence that satisfies a specified condition.
public static TSource First<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
The first element in source that passes the test in predicate .
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
No element satisfies the condition in predicate .
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use First<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to return the first element of a sequence that satisfies a condition.
// Get the first number in the array that is greater than 80.
int first = numbers.AsQueryable().First(number => number > 80);
Console.WriteLine(first);
/*
This code produces the following output:
92
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The First<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling First<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling First<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it returns the first element in source that satisfies the condition
specified by predicate .
Queryable.FirstOrDefault
I n this Article
Overloads
FirstOrDefault<TSource>(IQueryable<TSource>)
Returns the first element of a sequence, or a default value if
the sequence contains no elements.
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Boolean>>) Returns the first element of a sequence that satisfies a
specified condition or a default value if no such element is
found.
FirstOrDefault<TSource>(IQueryable<TSource>)
Returns the first element of a sequence, or a default value if the sequence contains no elements.
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The IQueryable<T> to return the first element of.
Returns
TSource
default ( TSource ) if source is empty; otherwise, the first element in source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use FirstOrDefault<TSource>(IQueryable<TSource>) on an empty
sequence.
// Create an empty array.
int[] numbers = { };
// Get the first item in the array, or else the
// default value for type int (0).
int first = numbers.AsQueryable().FirstOrDefault();
Console.WriteLine(first);
/*
This code produces the following output:
0
*/
Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no
elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use
the DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method to specify the default value that you want to
use if the collection is empty. Then, call First<TSource>(IQueryable<TSource>) to obtain the first element. The
following code example uses both techniques to obtain a default value of 1 if a collection of numeric months is empty.
Because the default value for an integer is 0, which does not correspond to any month, the default value must be
specified as 1 instead. The first result variable is checked for the unwanted default value after the query is completed.
The second result variable is obtained by calling DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) to specify a
default value of 1.
List<int> months = new List<int> { };
/*
This code produces the following output:
Remarks
The FirstOrDefault<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents
calling FirstOrDefault<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
FirstOrDefault<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter.
The expected behavior is that it returns the first element in source , or a default value if source is empty.
The FirstOrDefault method does not provide a way to specify the default value to return if source is empty. If you want
to specify a default value other than default(TSource) , use the DefaultIfEmpty<TSource>(IQueryable<TSource>,
TSource) method as described in the Example section.
FirstOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found.
public static TSource FirstOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if sourceis empty or if no element passes the test specified by predicate ; otherwise, the first
element in source that passes the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use FirstOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) by passing in a predicate. In the second query, there is no element in the
sequence that satisfies the condition.
string[] names = { "Hartono, Tommy", "Adams, Terry",
"Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
Console.WriteLine(
"There is {0} name that is longer than 30 characters.",
string.IsNullOrEmpty(firstVeryLongName) ? "NOT a" : "a");
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling FirstOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
FirstOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the
implementation of the type of the source parameter. The expected behavior is that it returns the first element in
source that satisfies the condition in predicate , or a default value if no element satisfies the condition.
Queryable.GroupBy
I n this Article
Overloads
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<
TSource>, Expression<Func<TSource,TKey>>, Expression< Groups the elements of a sequence according to a specified
Func<TSource,TElement>>, Expression<Func<TKey, key selector function and creates a result value from each
IEnumerable<TElement>,TResult>>, IEqualityComparer< group and its key. Keys are compared by using a specified
TKey>) comparer and the elements of each group are projected by
using a specified function.
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<
TSource>, Expression<Func<TSource,TKey>>, Expression< Groups the elements of a sequence according to a specified
Func<TSource,TElement>>, Expression<Func<TKey, key selector function and creates a result value from each
IEnumerable<TElement>,TResult>>) group and its key. The elements of each group are projected
by using a specified function.
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func< Groups the elements of a sequence according to a specified
TSource,TElement>>) key selector function and projects the elements for each group
by using a specified function.
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func< Groups the elements of a sequence and projects the elements
TSource,TElement>>, IEqualityComparer<TKey>) for each group by using a specified function. Key values are
compared by using a specified comparer.
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func<TKey, Groups the elements of a sequence according to a specified
IEnumerable<TSource>,TResult>>) key selector function and creates a result value from each
group and its key.
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func<TKey, Groups the elements of a sequence according to a specified
IEnumerable<TSource>,TResult>>, IEqualityComparer< key selector function and creates a result value from each
TKey>) group and its key. Keys are compared by using a specified
comparer.
GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<
Func<TSource,TKey>>, IEqualityComparer<TKey>) Groups the elements of a sequence according to a specified
key selector function and compares the keys by using a
specified comparer.
GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<
Func<TSource,TKey>>) Groups the elements of a sequence according to a specified
key selector function.
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>,
Expression<Func<TKey,IEnumerable<TElement>,TResult>>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. Keys are compared by using a specified comparer and the elements of each group are projected by
using a specified function.
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector,
System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResul
t>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
TElement
The type of the elements in each IGrouping<TKey,TElement>.
TResult
The type of the result value returned by resultSelector .
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector Expression<Func<TKey,IEnumerable<TElement>,TResult>>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a
projection over a group and its key.
Exceptions
ArgumentNullException
source or keySelector or elementSelector or resultSelector or comparer is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>,
IEqualityComparer<TKey>) method generates a MethodCallExpression that represents calling
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>,
IEqualityComparer<TKey>) itself as a constructed generic method. It then passes the MethodCallExpression to the
CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>,
IEqualityComparer<TKey>) depends on the implementation of the type of the source parameter. The expected
behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each
element. The comparer parameter is used to compare key values. The elementSelector parameter is used to project
the elements of each group, and the resultSelector parameter is used to obtain a result value from each group and
its key.
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>,
Expression<Func<TKey,IEnumerable<TElement>,TResult>>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. The elements of each group are projected by using a specified function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
TElement
The type of the elements in each IGrouping<TKey,TElement>.
TResult
The type of the result value returned by resultSelector .
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
resultSelector Expression<Func<TKey,IEnumerable<TElement>,TResult>>
A function to create a result value from each group.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a
projection over a group and its key.
Exceptions
ArgumentNullException
source or keySelector or elementSelector or resultSelector is null .
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey,TElement,TResult>
(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>,
Expression<Func<TKey,IEnumerable<TElement>,TResult>>) to group the elements of a sequence and project a
sequence of results of type TResult .
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>) method
generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TElement,TResult>
(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>,
Expression<Func<TKey,IEnumerable<TElement>,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>) depends on the
implementation of the type of the source parameter. The expected behavior is that it groups the elements of source
by key values that are obtained by invoking keySelector on each element. The elementSelector parameter is used to
project the elements of each group, and the resultSelector parameter is used to obtain a result value from each
group and its key.
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>)
Groups the elements of a sequence according to a specified key selector function and projects the elements for each
group by using a specified function.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
TElement
The type of the elements in each IGrouping<TKey,TElement>.
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
Returns
IQueryable<IGrouping<TKey,TElement>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> contains a sequence of objects of type TElement and a key.
Exceptions
ArgumentNullException
source or keySelector or elementSelector is null .
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey,TElement>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) to group the elements of a sequence.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
8
Barley
4
Boots
Daisy
1
Whiskers
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>) method generates a MethodCallExpression that represents calling
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector
on each element. It invokes elementSelector on each element to obtain a result element.
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>,
IEqualityComparer<TKey>)
Groups the elements of a sequence and projects the elements for each group by using a specified function. Key values
are compared by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
TElement
The type of the elements in each IGrouping<TKey,TElement>.
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
elementSelector Expression<Func<TSource,TElement>>
A function to map each source element to an element in an IGrouping<TKey,TElement>.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<IGrouping<TKey,TElement>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each
IGrouping<TKey,TElement> contains a sequence of objects of type TElement and a key.
Exceptions
ArgumentNullException
source or keySelector or elementSelector or comparer is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) method generates a MethodCallExpression that
represents calling GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) itself as a constructed generic method. It then
passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider
represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) depends on the implementation of the type of the
source parameter. The expected behavior is that it groups the elements of source by a key value that is obtained by
invoking keySelector on each element. Key values are compared by using comparer . The elementSelector
parameter is invoked on each element to obtain a result element.
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
TResult
The type of the result value returned by resultSelector .
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
resultSelector Expression<Func<TKey,IEnumerable<TSource>,TResult>>
A function to create a result value from each group.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a
projection over a group and its key.
Exceptions
ArgumentNullException
source or keySelector or resultSelector is null .
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) to group the elements
of a sequence and project a sequence of results of type TResult .
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>) method generates a MethodCallExpression that represents
calling GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>) depends on the implementation of the type of the source
parameter. The expected behavior is that it groups the elements of source by a key value that is obtained by invoking
keySelector on each element. The resultSelector parameter is used to obtain a result value from each group and
its key.
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>,
IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and creates a result value from each
group and its key. Keys are compared by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
TResult
The type of the result value returned by resultSelector .
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
resultSelector Expression<Func<TKey,IEnumerable<TSource>,TResult>>
A function to create a result value from each group.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<TResult>
An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a
projection over a group and its key.
Exceptions
ArgumentNullException
source or keySelector or resultSelector or comparer is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) method generates a
MethodCallExpression that represents calling GroupBy<TSource,TKey,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>,
IEqualityComparer<TKey>) itself as a constructed generic method. It then passes the MethodCallExpression to the
CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) depends on the
implementation of the type of the source parameter. The expected behavior is that it groups the elements of source
by key values that are obtained by invoking keySelector on each element. The comparer parameter is used to
compare keys and the resultSelector parameter is used to obtain a result value from each group and its key.
GroupBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)
Groups the elements of a sequence according to a specified key selector function and compares the keys by using a
specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to compare keys.
Returns
IQueryable<IGrouping<TKey,TSource>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> contains a sequence of objects and a key.
Exceptions
ArgumentNullException
source or keySelector or comparer is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)
method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) itself as a constructed generic method. It then passes
the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by
the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)
depends on the implementation of the type of the source parameter. The expected behavior is that it groups the
elements of source by a key value. The key value is obtained by invoking keySelector on each element, and key
values are compared by using comparer .
GroupBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>)
Groups the elements of a sequence according to a specified key selector function.
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey>
(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented in keySelector .
Parameters
source IQueryable<TSource>
An IQueryable<T> whose elements to group.
keySelector Expression<Func<TSource,TKey>>
A function to extract the key for each element.
Returns
IQueryable<IGrouping<TKey,TSource>>
An IQueryable<>> in C# or IQueryable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each
IGrouping<TKey,TElement> object contains a sequence of objects and a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use GroupBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>) to group the elements of a sequence.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) method generates a
MethodCallExpression that represents calling GroupBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on the implementation
of the type of the source parameter. The expected behavior is that it groups the elements of source by a key value
that is obtained by invoking keySelector on each element.
Queryable.GroupJoin
I n this Article
Overloads
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<
TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, Correlates the elements of two sequences based on key
TKey>>, Expression<Func<TInner,TKey>>, Expression<Func< equality and groups the results. The default equality comparer
TOuter,IEnumerable<TInner>,TResult>>) is used to compare keys.
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<
TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, Correlates the elements of two sequences based on key
TKey>>, Expression<Func<TInner,TKey>>, Expression<Func< equality and groups the results. A specified
TOuter,IEnumerable<TInner>,TResult>>, IEqualityComparer< IEqualityComparer<T> is used to compare keys.
TKey>)
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>)
Correlates the elements of two sequences based on key equality and groups the results. The default equality comparer
is used to compare keys.
public static System.Linq.IQueryable<TResult> GroupJoin<TOuter,TInner,TKey,TResult> (this
System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
System.Linq.Expressions.Expression<Func<TOuter,TKey>> outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey>> innerKeySelector,
System.Linq.Expressions.Expression<Func<TOuter,System.Collections.Generic.IEnumerable<TInner>,TResul
t>> resultSelector);
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,IEnumerable<TInner>,TResult>>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
Returns
IQueryable<TResult>
An IQueryable<T> that contains elements of type TResult obtained by performing a grouped join on two sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Examples
The following code example demonstrates how to use GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>) to perform a grouped join on two sequences.
class Person
{
public string Name { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
/*
This code produces the following output:
Hedlund, Magnus:
Daisy
Adams, Terry:
Barley
Boots
Weiss, Charlotte:
Whiskers
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>) method generates a MethodCallExpression that represents
calling GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the outer parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>) depends on the implementation of the type of the outer
parameter. The expected behavior is that the outerKeySelector and innerKeySelector functions are used to extract
keys from outer and inner , respectively. These keys are compared for equality to match each element in outer with
zero or more elements from inner . Then the resultSelector function is invoked to project a result object from each
group of correlated elements.
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>,
IEqualityComparer<TKey>)
Correlates the elements of two sequences based on key equality and groups the results. A specified
IEqualityComparer<T> is used to compare keys.
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,IEnumerable<TInner>,TResult>>
A function to create a result element from an element from the first sequence and a collection of matching elements
from the second sequence.
comparer IEqualityComparer<TKey>
A comparer to hash and compare keys.
Returns
IQueryable<TResult>
An IQueryable<T> that contains elements of type TResult obtained by performing a grouped join on two sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>, IEqualityComparer<TKey>) method generates a
MethodCallExpression that represents calling GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>, IEqualityComparer<TKey>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the outer parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
GroupJoin<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>,
Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,IEnumerable<TInner>,TResult>>, IEqualityComparer<TKey>) depends on the
implementation of the type of the outer parameter. The expected behavior is that the outerKeySelector and
innerKeySelector functions are used to extract keys from outer and inner , respectively. These keys are compared
for equality by using comparer . The outcome of the comparisons is used to match each element in outer with zero or
more elements from inner . Then the resultSelector function is invoked to project a result object from each group
of correlated elements.
Queryable.Intersect
I n this Article
Overloads
Intersect<TSource>(IQueryable<TSource>, IEnumerable<
TSource>) Produces the set intersection of two sequences by using the
default equality comparer to compare values.
Intersect<TSource>(IQueryable<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) Produces the set intersection of two sequences by using the
specified IEqualityComparer<T> to compare values.
Intersect<TSource>(IQueryable<TSource>,
IEnumerable<TSource>)
Produces the set intersection of two sequences by using the default equality comparer to compare values.
public static System.Linq.IQueryable<TSource> Intersect<TSource> (this
System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
A sequence whose distinct elements that also appear in source2 are returned.
source2 IEnumerable<TSource>
A sequence whose distinct elements that also appear in the first sequence are returned.
Returns
IQueryable<TSource>
A sequence that contains the set intersection of the two sequences.
Exceptions
ArgumentNullException
source1 or source2 is null .
Examples
The following code example demonstrates how to use Intersect<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) to return the elements that appear in each of two sequences.
int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };
// Get the numbers that occur in both arrays (id1 and id2).
IEnumerable<int> both = id1.AsQueryable().Intersect(id2);
/*
This code produces the following output:
26
30
*/
Remarks
The Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>) method generates a MethodCallExpression
that represents calling Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>) itself as a constructed
generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Intersect<TSource>
(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type of the source1
parameter. The expected behavior is that all the elements in source1 that are also in source2 are returned.
Intersect<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
Produces the set intersection of two sequences by using the specified IEqualityComparer<T> to compare values.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose distinct elements that also appear in source2 are returned.
source2 IEnumerable<TSource>
An IEnumerable<T> whose distinct elements that also appear in the first sequence are returned.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the set intersection of the two sequences.
Exceptions
ArgumentNullException
source1 or source2 is null .
Remarks
The Intersect<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method
generates a MethodCallExpression that represents calling Intersect<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Intersect<TSource>
(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) depends on the implementation of the
type of the source1 parameter. The expected behavior is that all the elements in source1 that are also in source2 are
returned. The comparer parameter is used to compare elements.
Queryable.Join
I n this Article
Overloads
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Correlates the elements of two sequences based on matching
Expression<Func<TInner,TKey>>, Expression<Func<TOuter, keys. The default equality comparer is used to compare keys.
TInner,TResult>>)
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Correlates the elements of two sequences based on matching
Expression<Func<TInner,TKey>>, Expression<Func<TOuter, keys. A specified IEqualityComparer<T> is used to compare
TInner,TResult>>, IEqualityComparer<TKey>) keys.
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,TInner,TResult>>)
Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare
keys.
public static System.Linq.IQueryable<TResult> Join<TOuter,TInner,TKey,TResult> (this
System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner,
System.Linq.Expressions.Expression<Func<TOuter,TKey>> outerKeySelector,
System.Linq.Expressions.Expression<Func<TInner,TKey>> innerKeySelector,
System.Linq.Expressions.Expression<Func<TOuter,TInner,TResult>> resultSelector);
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,TInner,TResult>>
A function to create a result element from two matching elements.
Returns
IQueryable<TResult>
An IQueryable<T> that has elements of type TResult obtained by performing an inner join on two sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Examples
The following code example demonstrates how to use Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,TInner,TResult>>) to perform an inner join of two sequences based on a common key.
class Person
{
public string Name { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
// Join the list of Person objects and the list of Pet objects
// to create a list of person-pet pairs where each element is
// an anonymous type that contains the name of pet and the name
// of the person that owns the pet.
var query = people.AsQueryable().Join(pets,
person => person,
pet => pet.Owner,
(person, pet) =>
new { OwnerName = person.Name, Pet = pet.Name });
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>, Expression<Func<TOuter,TInner,TResult>>) method generates a
MethodCallExpression that represents calling Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,TInner,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the outer parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>, Expression<Func<TOuter,TInner,TResult>>) depends on the implementation of the
type of the outer parameter. The expected behavior is that of an inner join. The outerKeySelector and
innerKeySelector functions are used to extract keys from outer and inner , respectively. These keys are compared
for equality to match elements from each sequence. A pair of elements is stored for each element in inner that
matches an element in outer . Then the resultSelector function is invoked to project a result object from each pair of
matching elements.
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,TInner,TResult>>,
IEqualityComparer<TKey>)
Correlates the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to
compare keys.
Type Parameters
TOuter
The type of the elements of the first sequence.
TInner
The type of the elements of the second sequence.
TKey
The type of the keys returned by the key selector functions.
TResult
The type of the result elements.
Parameters
outer IQueryable<TOuter>
The first sequence to join.
inner IEnumerable<TInner>
The sequence to join to the first sequence.
outerKeySelector Expression<Func<TOuter,TKey>>
A function to extract the join key from each element of the first sequence.
innerKeySelector Expression<Func<TInner,TKey>>
A function to extract the join key from each element of the second sequence.
resultSelector Expression<Func<TOuter,TInner,TResult>>
A function to create a result element from two matching elements.
comparer IEqualityComparer<TKey>
An IEqualityComparer<T> to hash and compare keys.
Returns
IQueryable<TResult>
An IQueryable<T> that has elements of type TResult obtained by performing an inner join on two sequences.
Exceptions
ArgumentNullException
outer or inner or outerKeySelector or innerKeySelector or resultSelector is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>, Expression<Func<TOuter,TInner,TResult>>, IEqualityComparer<TKey>) method
generates a MethodCallExpression that represents calling Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>,
IEnumerable<TInner>, Expression<Func<TOuter,TKey>>, Expression<Func<TInner,TKey>>,
Expression<Func<TOuter,TInner,TResult>>, IEqualityComparer<TKey>) itself as a constructed generic method. It then
passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider
represented by the Provider property of the outer parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Join<TOuter,TInner,TKey,TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter,TKey>>,
Expression<Func<TInner,TKey>>, Expression<Func<TOuter,TInner,TResult>>, IEqualityComparer<TKey>) depends on
the implementation of the type of the outer parameter. The expected behavior is that of an inner join. The
outerKeySelector and innerKeySelector functions are used to extract keys from outer and inner , respectively.
These keys are compared for equality by using comparer . The outcome of the comparisons is used to create a
matching pair for each element in inner that matches an element in outer . Then the resultSelector function is
invoked to project a result object from each pair of matching elements.
Queryable.Last
I n this Article
Overloads
Last<TSource>(IQueryable<TSource>)
Returns the last element in a sequence.
Last<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Boolean>>) Returns the last element of a sequence that satisfies a
specified condition.
Last<TSource>(IQueryable<TSource>)
Returns the last element in a sequence.
public static TSource Last<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the last element of.
Returns
TSource
The value at the last position in source .
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
The source sequence is empty.
Examples
The following code example demonstrates how to use Last<TSource>(IQueryable<TSource>) to return the last
element of an array.
int[] numbers = { 9, 34, 65, 92, 87, 435, 3, 54,
83, 23, 87, 67, 12, 19 };
Console.WriteLine(last);
/*
This code produces the following output:
19
*/
Remarks
The Last<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Last<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Last<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it returns the last element in source .
Last<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the last element of a sequence that satisfies a specified condition.
public static TSource Last<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
The last element in source that passes the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
No element satisfies the condition in predicate .
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use Last<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to return the last element of an array that satisfies a condition.
// Get the last number in the array that is greater than 80.
int last = numbers.AsQueryable().Last(num => num > 80);
Console.WriteLine(last);
/*
This code produces the following output:
87
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Last<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling Last<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Last<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it returns the last element in source that satisfies the condition
specified by predicate .
Queryable.LastOrDefault
I n this Article
Overloads
LastOrDefault<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Boolean>>) Returns the last element of a sequence that satisfies a
condition or a default value if no such element is found.
LastOrDefault<TSource>(IQueryable<TSource>)
Returns the last element in a sequence, or a default value if
the sequence contains no elements.
LastOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
public static TSource LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return an element from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
TSource
default ( TSource ) if source
is empty or if no elements pass the test in the predicate function; otherwise, the last
element of source that passes the test in the predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use LastOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) by passing in a predicate. In the second call to the method, there is no element
in the sequence that satisfies the condition.
double[] numbers = { 49.6, 52.3, 51.0, 49.4, 50.2, 48.3 };
Console.WriteLine(
"The last number that rounds to 40 is {0}.",
last40 == 0.0 ? "[DOES NOT EXIST]" : last40.ToString());
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The LastOrDefault<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
LastOrDefault<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
LastOrDefault<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter.
The expected behavior is that it returns the last element in source that satisfies the condition specified by predicate .
It returns a default value if there is no such element in source .
LastOrDefault<TSource>(IQueryable<TSource>)
Returns the last element in a sequence, or a default value if the sequence contains no elements.
public static TSource LastOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the last element of.
Returns
TSource
default ( TSource ) if source is empty; otherwise, the last element in source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use LastOrDefault<TSource>(IQueryable<TSource>) on an empty
array.
Console.WriteLine(
String.IsNullOrEmpty(last) ? "[STRING IS NULL OR EMPTY]" : last);
/*
This code produces the following output:
Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no
elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use
the DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method to specify the default value that you want to
use if the collection is empty. Then, call Last<TSource>(IQueryable<TSource>) to obtain the last element. The following
code example uses both techniques to obtain a default value of 1 if a collection of numeric days of the month is empty.
Because the default value for an integer is 0, which does not correspond to any day of the month, the default value
must be specified as 1 instead. The first result variable is checked for the unwanted default value after the query is
completed. The second result variable is obtained by calling DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource)
to specify a default value of 1.
List<int> daysOfMonth = new List<int> { };
/*
This code produces the following output:
The query behavior that occurs as a result of executing an expression tree that represents calling
LastOrDefault<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter.
The expected behavior is that it returns the last element in source , or a default value if source is empty.
The LastOrDefault method does not provide a way to specify a default value. If you want to specify a default value other
than default(TSource) , use the DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method as described in
the Example section.
Queryable.LongCount
I n this Article
Overloads
LongCount<TSource>(IQueryable<TSource>)
Returns an Int64 that represents the total number of elements
in a sequence.
LongCount<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Boolean>>) Returns an Int64 that represents the number of elements in a
sequence that satisfy a condition.
LongCount<TSource>(IQueryable<TSource>)
Returns an Int64 that represents the total number of elements in a sequence.
public static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
Returns
Int64
The number of elements in source .
Exceptions
ArgumentNullException
source is null .
OverflowException
The number of elements exceeds MaxValue.
Examples
The following code example demonstrates how to use LongCount<TSource>(IQueryable<TSource>) to count the
elements in an array.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
/*
This code produces the following output:
Remarks
The LongCount<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
LongCount<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling LongCount<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it counts the number of items in source and returns an Int64.
LongCount<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns an Int64 that represents the number of elements in a sequence that satisfy a condition.
public static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> that contains the elements to be counted.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
Int64
The number of elements in source that satisfy the condition in the predicate function.
Exceptions
ArgumentNullException
source or predicate is null .
OverflowException
The number of matching elements exceeds MaxValue.
Examples
The following code example demonstrates how to use LongCount<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to count the elements in an array that satisfy a condition.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling LongCount<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling LongCount<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it counts the number of items in source that satisfy the condition
specified by predicate and returns an Int64.
Queryable.Max
I n this Article
Overloads
Max<TSource,TResult>(IQueryable<TSource>, Expression<
Func<TSource,TResult>>) Invokes a projection function on each element of a generic
IQueryable<T> and returns the maximum resulting value.
Max<TSource>(IQueryable<TSource>)
Returns the maximum value in a generic IQueryable<T>.
Max<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>)
Invokes a projection function on each element of a generic IQueryable<T> and returns the maximum resulting value.
public static TResult Max<TSource,TResult> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by the function represented by selector .
Parameters
source IQueryable<TSource>
A sequence of values to determine the maximum of.
selector Expression<Func<TSource,TResult>>
A projection function to apply to each element.
Returns
TResult
The maximum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Max<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>) to determine the maximum value in a sequence of projected values.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The maximum pet age plus name length is {0}.",
max);
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) method generates a
MethodCallExpression that represents calling Max<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Max<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) depends on the implementation
of the type of the source parameter. The expected behavior is that it invokes selector on each element in source
and returns the maximum value.
Max<TSource>(IQueryable<TSource>)
Returns the maximum value in a generic IQueryable<T>.
public static TSource Max<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to determine the maximum of.
Returns
TSource
The maximum value in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Max<TSource>(IQueryable<TSource>) to determine the
maximum value in a sequence.
/*
This code produces the following output:
Remarks
The Max<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Max<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Max<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it returns the maximum value in source .
Queryable.Min
I n this Article
Overloads
Min<TSource,TResult>(IQueryable<TSource>, Expression<
Func<TSource,TResult>>) Invokes a projection function on each element of a generic
IQueryable<T> and returns the minimum resulting value.
Min<TSource>(IQueryable<TSource>)
Returns the minimum value of a generic IQueryable<T>.
Min<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>)
Invokes a projection function on each element of a generic IQueryable<T> and returns the minimum resulting value.
public static TResult Min<TSource,TResult> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TResult>> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by the function represented by selector .
Parameters
source IQueryable<TSource>
A sequence of values to determine the minimum of.
selector Expression<Func<TSource,TResult>>
A projection function to apply to each element.
Returns
TResult
The minimum value in the sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Min<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>) to determine the minimum value in a sequence of projected values.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
// Get the Pet object that has the smallest Age value.
int min = pets.AsQueryable().Min(pet => pet.Age);
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Min<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) method generates a
MethodCallExpression that represents calling Min<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Min<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) depends on the implementation
of the type of the source parameter. The expected behavior is that it invokes selector on each element in source
and returns the minimum value.
Min<TSource>(IQueryable<TSource>)
Returns the minimum value of a generic IQueryable<T>.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to determine the minimum of.
Returns
TSource
The minimum value in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Min<TSource>(IQueryable<TSource>) to determine the
minimum value in a sequence.
/*
This code produces the following output:
Remarks
The Min<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Min<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Min<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it returns the minimum value in source .
Queryable.OfType
I n this Article
Filters the elements of an IQueryable based on a specified type.
public static System.Linq.IQueryable<TResult> OfType<TResult> (this System.Linq.IQueryable source);
Type Parameters
TResult
The type to filter the elements of the sequence on.
Parameters
source IQueryable
An IQueryable whose elements to filter.
Returns
IQueryable<TResult>
A collection that contains the elements from source that have type TResult .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use OfType to filter out elements that are not of type PropertyInfo
from a list of elements of type MemberInfo.
/*
This code produces the following output:
Remarks
The OfType method generates a MethodCallExpression that represents calling OfType itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider
represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling OfType depends on
the implementation of the type of the source parameter. The expected behavior is that it filters out any elements in
source that are not of type TResult .
Queryable.OrderBy
I n this Article
Overloads
OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<
Func<TSource,TKey>>, IComparer<TKey>) Sorts the elements of a sequence in ascending order by using
a specified comparer.
OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<
Func<TSource,TKey>>) Sorts the elements of a sequence in ascending order according
to a key.
OrderBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>)
Sorts the elements of a sequence in ascending order by using a specified comparer.
public static System.Linq.IOrderedQueryable<TSource> OrderBy<TSource,TKey> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector, System.Collections.Generic.IComparer<TKey> comparer);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function that is represented by keySelector .
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector or comparer is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) method
generates a MethodCallExpression that represents calling OrderBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter. The result of calling CreateQuery<TElement>(Expression) is cast to type
IOrderedQueryable<T> and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) depends on
the implementation of the type of the source parameter. The expected behavior is that it sorts the elements of source
based on the key obtained by invoking keySelector on each element of source . The comparer parameter is used to
compare keys.
OrderBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>)
Sorts the elements of a sequence in ascending order according to a key.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function that is represented by keySelector .
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use OrderBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>) to sort the elements of a sequence.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Whiskers - 1
Boots - 4
Barley - 8
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) method generates a
MethodCallExpression that represents calling OrderBy<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter. The result of calling CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T>
and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on the implementation
of the type of the source parameter. The expected behavior is that it sorts the elements of source based on the key
obtained by invoking keySelector on each element of source .
Queryable.OrderByDescending
I n this Article
Overloads
OrderByDescending<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>) Sorts the elements of a sequence in descending order
according to a key.
OrderByDescending<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>) Sorts the elements of a sequence in descending order by using
a specified comparer.
OrderByDescending<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>)
Sorts the elements of a sequence in descending order according to a key.
public static System.Linq.IOrderedQueryable<TSource> OrderByDescending<TSource,TKey> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>>
keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function that is represented by keySelector .
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The OrderByDescending<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) method
generates a MethodCallExpression that represents calling OrderByDescending<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter. The result of calling CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T>
and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
OrderByDescending<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on the
implementation of the type of the source parameter. The expected behavior is that it sorts the elements of source in
descending order, based on the key obtained by invoking keySelector on each element of source .
OrderByDescending<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>)
Sorts the elements of a sequence in descending order by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function that is represented by keySelector .
Parameters
source IQueryable<TSource>
A sequence of values to order.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from an element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector or comparer is null .
Examples
The following code example demonstrates how to use OrderByDescending<TSource,TKey>(IQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>) to sort the elements of a sequence in descending order by
using a custom comparer.
/// <summary>
/// This IComparer class sorts by the fractional part of the decimal number.
/// </summary>
public class SpecialComparer : IComparer<decimal>
{
/// <summary>
/// Compare two decimal numbers by their fractional parts.
/// </summary>
/// <param name="d1">The first decimal to compare.</param>
/// <param name="d2">The second decimal to compare.</param>
/// <returns>1 if the first decimal's fractional part
/// is greater than the second decimal's fractional part,
/// -1 if the first decimal's fractional
/// part is less than the second decimal's fractional part,
/// or the result of calling Decimal.Compare()
/// if the fractional parts are equal.</returns>
public int Compare(decimal d1, decimal d2)
{
decimal fractional1, fractional2;
if (fractional1 == fractional2)
return Decimal.Compare(d1, d2);
else if (fractional1 > fractional2)
return 1;
else
return -1;
}
}
/*
This code produces the following output:
9.7
0.5
8.3
6.3
1.3
6.2
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The OrderByDescending<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>,
IComparer<TKey>) method generates a MethodCallExpression that represents calling
OrderByDescending<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)
itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>
(Expression) method of the IQueryProvider represented by the Provider property of the source parameter. The result
of calling CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T> and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
OrderByDescending<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)
depends on the implementation of the type of the source parameter. The expected behavior is that it sorts the
elements of source in descending order, based on the key obtained by invoking keySelector on each element of
source . The comparer parameter is used to compare keys.
Queryable.Prepend
I n this Article
public static System.Linq.IQueryable<TSource> Prepend<TSource> (this System.Linq.IQueryable<TSource>
source, TSource element);
Type Parameters
TSource
Parameters
source IQueryable<TSource>
element TSource
Returns
IQueryable<TSource>
Queryable.Reverse
I n this Article
Inverts the order of the elements in a sequence.
public static System.Linq.IQueryable<TSource> Reverse<TSource> (this System.Linq.IQueryable<TSource>
source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values to reverse.
Returns
IQueryable<TSource>
An IQueryable<T> whose elements correspond to those of the input sequence in reverse order.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Reverse<TSource>(IQueryable<TSource>) to reverse the order
of elements in an array.
char[] apple = { 'a', 'p', 'p', 'l', 'e' };
/*
This code produces the following output:
e l p p a
*/
Remarks
The Reverse<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Reverse<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Reverse<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it reverses the order of the elements in source .
Queryable.Select
I n this Article
Overloads
Select<TSource,TResult>(IQueryable<TSource>, Expression<
Func<TSource,Int32,TResult>>) Projects each element of a sequence into a new form by
incorporating the element's index.
Select<TSource,TResult>(IQueryable<TSource>, Expression<
Func<TSource,TResult>>) Projects each element of a sequence into a new form.
Select<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,TResult>>)
Projects each element of a sequence into a new form by incorporating the element's index.
public static System.Linq.IQueryable<TResult> Select<TSource,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,TResult>> selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by the function represented by selector .
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,Int32,TResult>>
A projection function to apply to each element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a projection function on each element of source .
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Select<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,TResult>>) to project over a sequence of values and use the index of each element in
the projected form.
string[] fruits = { "apple", "banana", "mango", "orange",
"passionfruit", "grape" };
/*
This code produces the following output:
{ index = 0, str = }
{ index = 1, str = b }
{ index = 2, str = ma }
{ index = 3, str = ora }
{ index = 4, str = pass }
{ index = 5, str = grape }
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) method generates a
MethodCallExpression that represents calling Select<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,TResult>>) depend on the
implementation of the type of the source parameter. The expected behavior is that it invokes selector on each
element of source to project it into a different form.
Select<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>)
Projects each element of a sequence into a new form.
public static System.Linq.IQueryable<TResult> Select<TSource,TResult> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TResult>>
selector);
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the value returned by the function represented by selector .
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,TResult>>
A projection function to apply to each element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a projection function on each element of source .
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Select<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>) to project over a sequence of values.
List<int> range =
new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
/*
This code produces the following output:
1
4
9
16
25
36
49
64
81
100
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) method generates a
MethodCallExpression that represents calling Select<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,TResult>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Select<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,TResult>>) depends on the
implementation of the type of the source parameter. The expected behavior is that it invokes selector on each
element of source to project it into a different form.
Queryable.SelectMany
I n this Article
Overloads
SelectMany<TSource,TCollection,TResult>(IQueryable<
TSource>, Expression<Func<TSource,Int32,IEnumerable< Projects each element of a sequence to an IEnumerable<T>
TCollection>>>, Expression<Func<TSource,TCollection, that incorporates the index of the source element that
TResult>>) produced it. A result selector function is invoked on each
element of each intermediate sequence, and the resulting
values are combined into a single, one-dimensional sequence
and returned.
SelectMany<TSource,TCollection,TResult>(IQueryable<
TSource>, Expression<Func<TSource,IEnumerable< Projects each element of a sequence to an IEnumerable<T>
TCollection>>>, Expression<Func<TSource,TCollection, and invokes a result selector function on each element therein.
TResult>>) The resulting values from each intermediate sequence are
combined into a single, one-dimensional sequence and
returned.
SelectMany<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TResult>>>) Projects each element of a sequence to an IEnumerable<T>
and combines the resulting sequences into one sequence. The
index of each source element is used in the projected form of
that element.
SelectMany<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TResult>>>) Projects each element of a sequence to an IEnumerable<T>
and combines the resulting sequences into one sequence.
SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TCollection>>>,
Expression<Func<TSource,TCollection,TResult>>)
Projects each element of a sequence to an IEnumerable<T> that incorporates the index of the source element that
produced it. A result selector function is invoked on each element of each intermediate sequence, and the resulting
values are combined into a single, one-dimensional sequence and returned.
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TCollection,TResult> (this
System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int,System.Collections.Generic.IEnumerable<TCollecti
on>>> collectionSelector, System.Linq.Expressions.Expression<Func<TSource,TCollection,TResult>>
resultSelector);
Type Parameters
TSource
The type of the elements of source .
TCollection
The type of the intermediate elements collected by the function represented by collectionSelector .
TResult
The type of the elements of the resulting sequence.
Parameters
source IQueryable<TSource>
A sequence of values to project.
collectionSelector Expression<Func<TSource,Int32,IEnumerable<TCollection>>>
A projection function to apply to each element of the input sequence; the second parameter of this function represents
the index of the source element.
resultSelector Expression<Func<TSource,TCollection,TResult>>
A projection function to apply to each element of each intermediate sequence.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking the one-to-many projection function
collectionSelector on each element of source and then mapping each of those sequence elements and their
corresponding source element to a result element.
Exceptions
ArgumentNullException
source or collectionSelector or resultSelector is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection,TResult>>)
method generates a MethodCallExpression that represents calling SelectMany<TSource,TCollection,TResult>
(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TCollection>>>,
Expression<Func<TSource,TCollection,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection,TResult>>)
depends on the implementation of the type of the source parameter. The expected behavior is that it invokes
collectionSelector on each element of source to project it into an enumerable form. Each enumerable result
incorporates the source element's index. Then the function represented by resultSelector is invoked on each
element in each intermediate sequence. The resulting values are concatenated into a single, one-dimensional sequence.
SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TCollection>>>,
Expression<Func<TSource,TCollection,TResult>>)
Projects each element of a sequence to an IEnumerable<T> and invokes a result selector function on each element
therein. The resulting values from each intermediate sequence are combined into a single, one-dimensional sequence
and returned.
Type Parameters
TSource
The type of the elements of source .
TCollection
The type of the intermediate elements collected by the function represented by collectionSelector .
TResult
The type of the elements of the resulting sequence.
Parameters
source IQueryable<TSource>
A sequence of values to project.
collectionSelector Expression<Func<TSource,IEnumerable<TCollection>>>
A projection function to apply to each element of the input sequence.
resultSelector Expression<Func<TSource,TCollection,TResult>>
A projection function to apply to each element of each intermediate sequence.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking the one-to-many projection function
collectionSelector on each element of source and then mapping each of those sequence elements and their
corresponding source element to a result element.
Exceptions
ArgumentNullException
source or collectionSelector or resultSelector is null .
Examples
The following code example demonstrates how to use SelectMany<TSource,TCollection,TResult>
(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TCollection>>>,
Expression<Func<TSource,TCollection,TResult>>) to perform a one-to-many projection over an array. This example
uses a result selector function to keep the source element that corresponds to each intermediate sequence in scope for
the final call to Select .
class PetOwner
{
public string Name { get; set; }
public List<Pet> Pets { get; set; }
}
class Pet
{
public string Name { get; set; }
public string Breed { get; set; }
}
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection,TResult>>) method
generates a MethodCallExpression that represents calling SelectMany<TSource,TCollection,TResult>
(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TCollection>>>,
Expression<Func<TSource,TCollection,TResult>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection,TResult>>) depends
on the implementation of the type of the source parameter. The expected behavior is that it invokes
collectionSelector on each element of source to project it into an enumerable form. Then the function represented
by resultSelector is invoked on each element in each intermediate sequence. The resulting values are concatenated
into a single, one-dimensional sequence.
SelectMany<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TResult>>>)
Projects each element of a sequence to an IEnumerable<T> and combines the resulting sequences into one sequence.
The index of each source element is used in the projected form of that element.
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the elements of the sequence returned by the function represented by selector .
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,Int32,IEnumerable<TResult>>>
A projection function to apply to each element; the second parameter of this function represents the index of the source
element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a one-to-many projection function on each element of
the input sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use SelectMany<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,Int32,IEnumerable<TResult>>>) to perform a one-to-many projection over an array and
use the index of each source element.
class PetOwner
{
public string Name { get; set; }
public List<string> Pets { get; set; }
}
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>)
method generates a MethodCallExpression that represents calling SelectMany<TSource,TResult>
(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider
represented by the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>)
depends on the implementation of the type of the source parameter. The expected behavior is that it invokes
selector on each element of source to project it into an enumerable form. Each enumerable result incorporates the
index of the source element. It then concatenates the enumerable results into a single, one-dimensional sequence.
SelectMany<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TResult>>>)
Projects each element of a sequence to an IEnumerable<T> and combines the resulting sequences into one sequence.
Type Parameters
TSource
The type of the elements of source .
TResult
The type of the elements of the sequence returned by the function represented by selector .
Parameters
source IQueryable<TSource>
A sequence of values to project.
selector Expression<Func<TSource,IEnumerable<TResult>>>
A projection function to apply to each element.
Returns
IQueryable<TResult>
An IQueryable<T> whose elements are the result of invoking a one-to-many projection function on each element of
the input sequence.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use SelectMany<TSource,TResult>(IQueryable<TSource>,
Expression<Func<TSource,IEnumerable<TResult>>>) to perform a one-to-many projection over an array.
class PetOwner
{
public string Name { get; set; }
public List<String> Pets { get; set; }
}
Console.WriteLine("Using SelectMany():");
Console.WriteLine("
Using Select():");
/*
This code produces the following output:
Using SelectMany():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
Using Select():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>)
method generates a MethodCallExpression that represents calling SelectMany<TSource,TResult>
(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) itself as a constructed generic method. It
then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by
the Provider property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) depends
on the implementation of the type of the source parameter. The expected behavior is that it invokes selector on each
element of source to project it into an enumerable form. It then concatenates the enumerable results into a single,
one-dimensional sequence.
Queryable.SequenceEqual
I n this Article
Overloads
SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) Determines whether two sequences are equal by using the
default equality comparer to compare elements.
SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) Determines whether two sequences are equal by using a
specified IEqualityComparer<T> to compare elements.
SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>)
Determines whether two sequences are equal by using the default equality comparer to compare elements.
public static bool SequenceEqual<TSource> (this System.Linq.IQueryable<TSource> source1,
System.Collections.Generic.IEnumerable<TSource> source2);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements to compare to those of source2 .
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements to compare to those of the first sequence.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements compare equal; otherwise,
false .
Exceptions
ArgumentNullException
source1 or source2 is null .
Examples
The following code example demonstrates how to use SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) to determine whether two sequences are equal. In this example the sequences are equal.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
Console.WriteLine(
"The lists {0} equal.",
equal ? "are" : "are not");
}
/*
This code produces the following output:
The following code example compares two sequences that are not equal.
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
/*
This code produces the following output:
Remarks
The SequenceEqual<TSource>(IQueryable<TSource>, IEnumerable<TSource>) method generates a
MethodCallExpression that represents calling SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source1
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SequenceEqual<TSource>(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type
of the source1 parameter. The expected behavior is that it determines if the two source sequences are equal.
SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>)
Determines whether two sequences are equal by using a specified IEqualityComparer<T> to compare elements.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
An IQueryable<T> whose elements to compare to those of source2 .
source2 IEnumerable<TSource>
An IEnumerable<T> whose elements to compare to those of the first sequence.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to use to compare elements.
Returns
Boolean
true if the two source sequences are of equal length and their corresponding elements compare equal; otherwise,
false .
Exceptions
ArgumentNullException
source1 or source2 is null .
Remarks
The SequenceEqual<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method
generates a MethodCallExpression that represents calling SequenceEqual<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SequenceEqual<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) depends on
the implementation of the type of the source1 parameter. The expected behavior is that it determines if the two source
sequences are equal by using comparer to compare elements.
Queryable.Single
I n this Article
Overloads
Single<TSource>(IQueryable<TSource>)
Returns the only element of a sequence, and throws an
exception if there is not exactly one element in the sequence.
Single<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Boolean>>) Returns the only element of a sequence that satisfies a
specified condition, and throws an exception if more than one
such element exists.
Single<TSource>(IQueryable<TSource>)
Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
public static TSource Single<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the single element of.
Returns
TSource
The single element of the input sequence.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source has more than one element.
Examples
The following code example demonstrates how to use Single<TSource>(IQueryable<TSource>) to select the only
element of an array.
// Create two arrays.
string[] fruits1 = { "orange" };
string[] fruits2 = { "orange", "apple" };
try
{
// Try to get the only item in the second array.
string fruit2 = fruits2.AsQueryable().Single();
Console.WriteLine("Second query: " + fruit2);
}
catch (System.InvalidOperationException)
{
Console.WriteLine(
"Second query: The collection does not contain exactly one element."
);
}
/*
This code produces the following output:
Remarks
The Single<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling
Single<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Single<TSource>
(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is
that it returns the only element in source .
Single<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one
such element exists.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return a single element from.
predicate Expression<Func<TSource,Boolean>>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition in predicate .
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
No element satisfies the condition in predicate .
-or-
More than one element satisfies the condition in predicate .
-or-
The source sequence is empty.
Examples
The following code example demonstrates how to use Single<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to select the only element of an array that satisfies a condition.
// Get the only string in the array whose length is greater than 10.
string fruit1 = fruits.AsQueryable().Single(fruit => fruit.Length > 10);
try
{
// Try to get the only string in the array
// whose length is greater than 15.
string fruit2 = fruits.AsQueryable().Single(fruit => fruit.Length > 15);
Console.WriteLine("Second Query: " + fruit2);
}
catch (System.InvalidOperationException)
{
Console.Write("Second Query: The collection does not contain ");
Console.WriteLine("exactly one element whose length is greater than 15.");
}
/*
This code produces the following output:
The query behavior that occurs as a result of executing an expression tree that represents calling Single<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it returns the only element in source that satisfies the condition
specified by predicate .
Queryable.SingleOrDefault
I n this Article
Overloads
SingleOrDefault<TSource>(IQueryable<TSource>)
Returns the only element of a sequence, or a default value if
the sequence is empty; this method throws an exception if
there is more than one element in the sequence.
SingleOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) Returns the only element of a sequence that satisfies a
specified condition or a default value if no such element exists;
this method throws an exception if more than one element
satisfies the condition.
SingleOrDefault<TSource>(IQueryable<TSource>)
Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if
there is more than one element in the sequence.
public static TSource SingleOrDefault<TSource> (this System.Linq.IQueryable<TSource> source);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return the single element of.
Returns
TSource
The single element of the input sequence, or default ( TSource ) if the sequence contains no elements.
Exceptions
ArgumentNullException
source is null .
InvalidOperationException
source has more than one element.
Examples
The following code example demonstrates how to use SingleOrDefault<TSource>(IQueryable<TSource>) to select the
only element of an array. The second query demonstrates that SingleOrDefault<TSource>(IQueryable<TSource>)
returns a default value when the sequence does not contain exactly one element.
// Create two arrays. The second is empty.
string[] fruits1 = { "orange" };
string[] fruits2 = { };
/*
This code produces the following output:
Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no
elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use
the DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method to specify the default value that you want to
use if the collection is empty. Then, call Single<TSource>(IQueryable<TSource>) to obtain the element. The following
code example uses both techniques to obtain a default value of 1 if a collection of page numbers is empty. Because the
default value for an integer is 0, which is not usually a valid page number, the default value must be specified as 1
instead. The first result variable is checked for the unwanted default value after the query is completed. The second
result variable is obtained by calling DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) to specify a default
value of 1.
int[] pageNumbers = { };
/*
This code produces the following output:
Remarks
The SingleOrDefault<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents
calling SingleOrDefault<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SingleOrDefault<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source
parameter. The expected behavior is that it returns the only element in source , or a default value if source is empty.
The SingleOrDefault method does not provide a way to specify a default value. If you want to specify a default value
other than default(TSource) , use the DefaultIfEmpty<TSource>(IQueryable<TSource>, TSource) method as
described in the Example section.
SingleOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists;
this method throws an exception if more than one element satisfies the condition.
public static TSource SingleOrDefault<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return a single element from.
predicate Expression<Func<TSource,Boolean>>
A function to test an element for a condition.
Returns
TSource
The single element of the input sequence that satisfies the condition in predicate , or default ( TSource ) if no such
element is found.
Exceptions
ArgumentNullException
source or predicate is null .
InvalidOperationException
More than one element satisfies the condition in predicate .
Examples
The following code example demonstrates how to use SingleOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to select the only element of an array that satisfies a condition. The second
query demonstrates that SingleOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
returns a default value when the sequence does not contain exactly one element that satisfies the condition.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SingleOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling SingleOrDefault<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
SingleOrDefault<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the
implementation of the type of the source parameter. The expected behavior is that it returns the only element in
source that satisfies the condition specified by predicate , or a default value if no such element exists.
Queryable.Skip
I n this Article
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
public static System.Linq.IQueryable<TSource> Skip<TSource> (this System.Linq.IQueryable<TSource>
source, int count);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return elements from.
count Int32
The number of elements to skip before returning the remaining elements.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements that occur after the specified index in the input sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Skip<TSource>(IQueryable<TSource>, Int32) to skip a specified
number of elements in a sorted array and return the remaining elements.
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
/*
This code produces the following output:
Type Parameters
TSource
Parameters
source IQueryable<TSource>
count Int32
Returns
IQueryable<TSource>
Queryable.SkipWhile
I n this Article
Overloads
SkipWhile<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Boolean>>) Bypasses elements in a sequence as long as a specified
condition is true and then returns the remaining elements.
SkipWhile<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Int32,Boolean>>) Bypasses elements in a sequence as long as a specified
condition is true and then returns the remaining elements. The
element's index is used in the logic of the predicate function.
SkipWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return elements from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from source starting at the first element in the linear series that does not
pass the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use SkipWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to skip elements of an array as long as a condition is true.
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling SkipWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling SkipWhile<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it applies predicate to each element in source until it finds an
element for which predicate returns false. That element and all the remaining elements are returned.
SkipWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>)
Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The
element's index is used in the logic of the predicate function.
public static System.Linq.IQueryable<TSource> SkipWhile<TSource> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>>
predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to return elements from.
predicate Expression<Func<TSource,Int32,Boolean>>
A function to test each element for a condition; the second parameter of this function represents the index of the source
element.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from source starting at the first element in the linear series that does not
pass the test specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use SkipWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>) to skip elements of an array as long as a condition that depends on the
element's index is true.
int[] amounts = { 5000, 2500, 9000, 8000,
6500, 4000, 1500, 5500 };
/*
This code produces the following output:
4000
1500
5500
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The SkipWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) method generates a
MethodCallExpression that represents calling SkipWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling SkipWhile<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it applies predicate to each element in source until it finds an
element for which predicate returns false. That element and all the remaining elements are returned. The index of
each source element is provided as the second argument to predicate .
Queryable.Sum
I n this Article
Overloads
Sum(IQueryable<Single>)
Computes the sum of a sequence of Single values.
Sum(IQueryable<Nullable<Single>>)
Computes the sum of a sequence of nullable Single values.
Sum(IQueryable<Nullable<Int64>>)
Computes the sum of a sequence of nullable Int64 values.
Sum(IQueryable<Nullable<Int32>>)
Computes the sum of a sequence of nullable Int32 values.
Sum(IQueryable<Nullable<Double>>)
Computes the sum of a sequence of nullable Double values.
Sum(IQueryable<Double>)
Computes the sum of a sequence of Double values.
Sum(IQueryable<Int64>)
Computes the sum of a sequence of Int64 values.
Sum(IQueryable<Int32>)
Computes the sum of a sequence of Int32 values.
Sum(IQueryable<Nullable<Decimal>>)
Computes the sum of a sequence of nullable Decimal values.
Sum(IQueryable<Decimal>)
Computes the sum of a sequence of Decimal values.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Single>>>) Computes the sum of the sequence of nullable Single values
that is obtained by invoking a projection function on each
element of the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Int64>>>) Computes the sum of the sequence of nullable Int64 values
that is obtained by invoking a projection function on each
element of the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Int32>>>) Computes the sum of the sequence of nullable Int32 values
that is obtained by invoking a projection function on each
element of the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Double>>>) Computes the sum of the sequence of nullable Double values
that is obtained by invoking a projection function on each
element of the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Single>>) Computes the sum of the sequence of Single values that is
obtained by invoking a projection function on each element of
the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Int64>>) Computes the sum of the sequence of Int64 values that is
obtained by invoking a projection function on each element of
the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Int32>>) Computes the sum of the sequence of Int32 values that is
obtained by invoking a projection function on each element of
the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Double>>) Computes the sum of the sequence of Double values that is
obtained by invoking a projection function on each element of
the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Decimal>>) Computes the sum of the sequence of Decimal values that is
obtained by invoking a projection function on each element of
the input sequence.
Sum<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Nullable<Decimal>>>) Computes the sum of the sequence of nullable Decimal values
that is obtained by invoking a projection function on each
element of the input sequence.
Sum(IQueryable<Single>)
Computes the sum of a sequence of Single values.
Parameters
source IQueryable<Single>
A sequence of Single values to calculate the sum of.
Returns
Single
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IQueryable<Single>) to sum the values of a sequence.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Single>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Single>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Single>) depends on the implementation of the type of the source parameter. The expected
behavior is that it returns the sum of the values in source .
Sum(IQueryable<Nullable<Single>>)
Computes the sum of a sequence of nullable Single values.
Parameters
source IQueryable<Nullable<Single>>
A sequence of nullable Single values to calculate the sum of.
Returns
Nullable<Single>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IQueryable<Nullable<Single>>) to sum the values of a
sequence.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Nullable<Single>>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Nullable<Single>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Nullable<Single>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it returns the sum of the values in source .
Sum(IQueryable<Nullable<Int64>>)
Computes the sum of a sequence of nullable Int64 values.
Parameters
source IQueryable<Nullable<Int64>>
A sequence of nullable Int64 values to calculate the sum of.
Returns
Nullable<Int64>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IQueryable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Nullable<Int64>>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Nullable<Int64>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Nullable<Int64>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it returns the sum of the values in source .
Sum(IQueryable<Nullable<Int32>>)
Computes the sum of a sequence of nullable Int32 values.
Parameters
source IQueryable<Nullable<Int32>>
A sequence of nullable Int32 values to calculate the sum of.
Returns
Nullable<Int32>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IQueryable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Nullable<Int32>>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Nullable<Int32>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Nullable<Int32>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it returns the sum of the values in source .
Sum(IQueryable<Nullable<Double>>)
Computes the sum of a sequence of nullable Double values.
Parameters
source IQueryable<Nullable<Double>>
A sequence of nullable Double values to calculate the sum of.
Returns
Nullable<Double>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IQueryable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Nullable<Double>>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Nullable<Double>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Nullable<Double>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it returns the sum of the values in source .
Sum(IQueryable<Double>)
Computes the sum of a sequence of Double values.
public static double Sum (this System.Linq.IQueryable<double> source);
Parameters
source IQueryable<Double>
A sequence of Double values to calculate the sum of.
Returns
Double
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Sum(IQueryable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Double>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Double>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Double>) depends on the implementation of the type of the source parameter. The expected
behavior is that it returns the sum of the values in source .
Sum(IQueryable<Int64>)
Computes the sum of a sequence of Int64 values.
Parameters
source IQueryable<Int64>
A sequence of Int64 values to calculate the sum of.
Returns
Int64
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IQueryable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Int64>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Int64>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Int64>) depends on the implementation of the type of the source parameter. The expected behavior
is that it returns the sum of the values in source .
Sum(IQueryable<Int32>)
Computes the sum of a sequence of Int32 values.
Parameters
source IQueryable<Int32>
A sequence of Int32 values to calculate the sum of.
Returns
Int32
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IQueryable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Int32>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Int32>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Int32>) depends on the implementation of the type of the source parameter. The expected behavior
is that it returns the sum of the values in source .
Sum(IQueryable<Nullable<Decimal>>)
Computes the sum of a sequence of nullable Decimal values.
Parameters
source IQueryable<Nullable<Decimal>>
A sequence of nullable Decimal values to calculate the sum of.
Returns
Nullable<Decimal>
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IQueryable<Nullable<Single>>) to sum the values of a
sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Nullable<Decimal>>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Nullable<Decimal>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Nullable<Decimal>>) depends on the implementation of the type of the source parameter. The
expected behavior is that it returns the sum of the values in source .
Sum(IQueryable<Decimal>)
Computes the sum of a sequence of Decimal values.
Parameters
source IQueryable<Decimal>
A sequence of Decimal values to calculate the sum of.
Returns
Decimal
The sum of the values in the sequence.
Exceptions
ArgumentNullException
source is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum(IQueryable<Single>) to sum the values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, substitute the elements of the source sequence with elements of
the appropriate numerical type.
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
/*
This code produces the following output:
Remarks
The Sum(IQueryable<Decimal>) method generates a MethodCallExpression that represents calling
Sum(IQueryable<Decimal>) itself as a constructed generic method. It then passes the MethodCallExpression to the
Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling
Sum(IQueryable<Decimal>) depends on the implementation of the type of the source parameter. The expected
behavior is that it returns the sum of the values in source .
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Single>>>)
Computes the sum of the sequence of nullable Single values that is obtained by invoking a projection function on each
element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Single>>>
A projection function to apply to each element.
Returns
Nullable<Single>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Single>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Single>>>) depends on the implementation of the type
of the source parameter. The expected behavior is that it invokes selector on each element of source and returns
the sum of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int64>>>)
Computes the sum of the sequence of nullable Int64 values that is obtained by invoking a projection function on each
element of the input sequence.
public static Nullable<long> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<long>>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Int64>>>
A projection function to apply to each element.
Returns
Nullable<Int64>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int64>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int64>>>) depends on the implementation of the type of
the source parameter. The expected behavior is that it invokes selector on each element of source and returns the
sum of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int32>>>)
Computes the sum of the sequence of nullable Int32 values that is obtained by invoking a projection function on each
element of the input sequence.
public static Nullable<int> Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,Nullable<int>>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Int32>>>
A projection function to apply to each element.
Returns
Nullable<Int32>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Int32>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Int32>>>) depends on the implementation of the type of
the source parameter. The expected behavior is that it invokes selector on each element of source and returns the
sum of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Double>>>)
Computes the sum of the sequence of nullable Double values that is obtained by invoking a projection function on each
element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Double>>>
A projection function to apply to each element.
Returns
Nullable<Double>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Double>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Double>>>) depends on the implementation of the type
of the source parameter. The expected behavior is that it invokes selector on each element of source and returns
the sum of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Single>>)
Computes the sum of the sequence of Single values that is obtained by invoking a projection function on each element
of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Single>>
A projection function to apply to each element.
Returns
Single
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Single>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Single>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Single>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it invokes selector on each element of source and returns the sum
of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int64>>)
Computes the sum of the sequence of Int64 values that is obtained by invoking a projection function on each element
of the input sequence.
public static long Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,long>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Int64>>
A projection function to apply to each element.
Returns
Int64
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int64>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int64>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int64>>) depends on the implementation of the type of the source
parameter. The expected behavior is that it invokes selector on each element of source and returns the sum of the
resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>)
Computes the sum of the sequence of Int32 values that is obtained by invoking a projection function on each element
of the input sequence.
public static int Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,int>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Int32>>
A projection function to apply to each element.
Returns
Int32
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int32>>) depends on the implementation of the type of the source
parameter. The expected behavior is that it invokes selector on each element of source and returns the sum of the
resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>)
Computes the sum of the sequence of Double values that is obtained by invoking a projection function on each
element of the input sequence.
public static double Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,double>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Double>>
A projection function to apply to each element.
Returns
Double
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Double>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Double>>) depends on the implementation of the type of he
source parameter. The expected behavior is that it invokes selector on each element of source and returns the sum
of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Decimal>>)
Computes the sum of the sequence of Decimal values that is obtained by invoking a projection function on each
element of the input sequence.
public static decimal Sum<TSource> (this System.Linq.IQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,decimal>> selector);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Decimal>>
A projection function to apply to each element.
Returns
Decimal
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Decimal>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Decimal>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Decimal>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it invokes selector on each element of source and returns the sum
of the resulting values.
Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Decimal>>>)
Computes the sum of the sequence of nullable Decimal values that is obtained by invoking a projection function on
each element of the input sequence.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
A sequence of values of type TSource .
selector Expression<Func<TSource,Nullable<Decimal>>>
A projection function to apply to each element.
Returns
Nullable<Decimal>
The sum of the projected values.
Exceptions
ArgumentNullException
source or selector is null .
OverflowException
The sum is larger than MaxValue.
Examples
The following code example demonstrates how to use Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Double>>) to sum the projected values of a sequence.
Note
This code example uses an overload of this overloaded method that is different from the specific overload that this
topic describes. To extend the example to this topic, change the body of the selector function.
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
/*
This code produces the following output:
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Sum<TSource>(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>) method generates a
MethodCallExpression that represents calling Sum<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Nullable<Decimal>>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the Execute<TResult>(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Sum<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Nullable<Decimal>>>) depends on the implementation of the type
of the source parameter. The expected behavior is that it invokes selector on each element of source and returns
the sum of the resulting values.
Queryable.Take
I n this Article
Returns a specified number of contiguous elements from the start of a sequence.
public static System.Linq.IQueryable<TSource> Take<TSource> (this System.Linq.IQueryable<TSource>
source, int count);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The sequence to return elements from.
count Int32
The number of elements to return.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the specified number of elements from the start of source .
Exceptions
ArgumentNullException
source is null .
Examples
The following code example demonstrates how to use Take<TSource>(IQueryable<TSource>, Int32) to return
elements from the start of a sequence.
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
// Sort the grades in descending order and take the first three.
IEnumerable<int> topThreeGrades =
grades.AsQueryable().OrderByDescending(grade => grade).Take(3);
/*
This code produces the following output:
Type Parameters
TSource
Parameters
source IQueryable<TSource>
count Int32
Returns
IQueryable<TSource>
Queryable.TakeWhile
I n this Article
Overloads
TakeWhile<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Int32,Boolean>>) Returns elements from a sequence as long as a specified
condition is true. The element's index is used in the logic of the
predicate function.
TakeWhile<TSource>(IQueryable<TSource>, Expression<
Func<TSource,Boolean>>) Returns elements from a sequence as long as a specified
condition is true.
TakeWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>)
Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of
the predicate function.
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>>
predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The sequence to return elements from.
predicate Expression<Func<TSource,Int32,Boolean>>
A function to test each element for a condition; the second parameter of the function represents the index of the
element in the source sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence occurring before the element at which the test
specified by predicate no longer passes.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use TakeWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>) to return elements from the start of a sequence as long as a condition
that uses the index of the element is true.
string[] fruits = { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
/*
This code produces the following output:
apple
passionfruit
banana
mango
orange
blueberry
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) method generates a
MethodCallExpression that represents calling TakeWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling TakeWhile<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it applies predicate to each element in source until it finds an
element for which predicate returns false . It returns all the elements up until that point. The index of each source
element is provided as the second argument to predicate .
TakeWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Returns elements from a sequence as long as a specified condition is true.
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this
System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>>
predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
The sequence to return elements from.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence occurring before the element at which the test
specified by predicate no longer passes.
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use TakeWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to return elements from the start of a sequence as long as a condition is true.
string[] fruits = { "apple", "banana", "mango", "orange",
"passionfruit", "grape" };
/*
This code produces the following output:
apple
banana
mango
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling TakeWhile<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling TakeWhile<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it applies predicate to each element in source until it finds an
element for which predicate returns false . It returns all the elements up until that point.
Queryable.ThenBy
I n this Article
Overloads
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>) Performs a subsequent ordering of the elements in a sequence
in ascending order according to a key.
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>) Performs a subsequent ordering of the elements in a sequence
in ascending order by using a specified comparer.
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>)
Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
public static System.Linq.IOrderedQueryable<TSource> ThenBy<TSource,TKey> (this
System.Linq.IOrderedQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented by keySelector .
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Examples
The following code example demonstrates how to use ThenBy<TSource,TKey>(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>) to perform a secondary ordering of the elements in a sequence.
string[] fruits = { "grape", "passionfruit", "banana", "apple",
"orange", "raspberry", "mango", "blueberry" };
/*
This code produces the following output:
apple
grape
mango
banana
orange
blueberry
raspberry
passionfruit
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) method generates a
MethodCallExpression that represents calling ThenBy<TSource,TKey>(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>) itself as a constructed generic method. It then passes the MethodCallExpression to
the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the
source parameter. The result of calling CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T>
and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on the
implementation of the type of the source parameter. The expected behavior is that it performs a secondary sort of the
elements of source based on the key obtained by invoking keySelector on each element of source . All previously
established sort orders are preserved.
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>)
Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented by keySelector .
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted according to a key.
Exceptions
ArgumentNullException
source or keySelector or comparer is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)
method generates a MethodCallExpression that represents calling ThenBy<TSource,TKey>
(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the source parameter. The result of calling
CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T> and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)
depends on the implementation of the type of the source parameter. The expected behavior is that it performs a
secondary sort of the elements of source based on the key obtained by invoking keySelector on each element of
source . All previously established sort orders are preserved. The comparer parameter is used to compare key values.
Queryable.ThenByDescending
I n this Article
Overloads
ThenByDescending<TSource,TKey>(IOrderedQueryable<
TSource>, Expression<Func<TSource,TKey>>) Performs a subsequent ordering of the elements in a sequence
in descending order, according to a key.
ThenByDescending<TSource,TKey>(IOrderedQueryable<
TSource>, Expression<Func<TSource,TKey>>, IComparer< Performs a subsequent ordering of the elements in a sequence
TKey>) in descending order by using a specified comparer.
ThenByDescending<TSource,TKey>
(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>)
Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey> (this
System.Linq.IOrderedQueryable<TSource> source,
System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key returned by the function represented by keySelector .
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
Returns
IOrderedQueryable<TSource>
An IOrderedQueryable<T> whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector is null .
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) method
generates a MethodCallExpression that represents calling ThenByDescending<TSource,TKey>
(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) itself as a constructed generic method. It then
passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider
represented by the Provider property of the source parameter. The result of calling CreateQuery<TElement>
(Expression) is cast to type IOrderedQueryable<T> and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on
the implementation of the type of the source parameter. The expected behavior is that it performs a secondary sort of
the elements of source in descending order, based on the key obtained by invoking keySelector on each element of
source . All previously established sort orders are preserved.
ThenByDescending<TSource,TKey>
(IOrderedQueryable<TSource>,
Expression<Func<TSource,TKey>>, IComparer<TKey>)
Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
Type Parameters
TSource
The type of the elements of source .
TKey
The type of the key that is returned by the keySelector function.
Parameters
source IOrderedQueryable<TSource>
An IOrderedQueryable<T> that contains elements to sort.
keySelector Expression<Func<TSource,TKey>>
A function to extract a key from each element.
comparer IComparer<TKey>
An IComparer<T> to compare keys.
Returns
IOrderedQueryable<TSource>
A collection whose elements are sorted in descending order according to a key.
Exceptions
ArgumentNullException
source or keySelector or comparer is null .
Examples
The following code example demonstrates how to use ThenByDescending<TSource,TKey>
(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) to perform a secondary
ordering of the elements in a sequence in descending order by using a custom comparer.
public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, true);
}
}
/*
This code produces the following output:
apPLe
apple
APple
apPLE
orange
ORANGE
baNanA
BAnana
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)
method generates a MethodCallExpression that represents calling ThenBy<TSource,TKey>
(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the source parameter. The result of calling
CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T> and returned.
The query behavior that occurs as a result of executing an expression tree that represents calling
ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)
depends on the implementation of the type of the source parameter. The expected behavior is that it performs a
secondary sort of the elements of source in descending order, based on the key obtained by invoking keySelector
on each element of source . All previously established sort orders are preserved. The comparer parameter is used to
compare key values.
Queryable.Union
I n this Article
Overloads
Union<TSource>(IQueryable<TSource>, IEnumerable<
TSource>) Produces the set union of two sequences by using the default
equality comparer.
Union<TSource>(IQueryable<TSource>, IEnumerable<
TSource>, IEqualityComparer<TSource>) Produces the set union of two sequences by using a specified
IEqualityComparer<T>.
Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>)
Produces the set union of two sequences by using the default equality comparer.
public static System.Linq.IQueryable<TSource> Union<TSource> (this System.Linq.IQueryable<TSource>
source1, System.Collections.Generic.IEnumerable<TSource> source2);
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
A sequence whose distinct elements form the first set for the union operation.
source2 IEnumerable<TSource>
A sequence whose distinct elements form the second set for the union operation.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the elements from both input sequences, excluding duplicates.
Exceptions
ArgumentNullException
source1 or source2 is null .
Examples
The following code example demonstrates how to use Union<TSource>(IQueryable<TSource>,
IEnumerable<TSource>) to obtain the set union of two sequences.
int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };
/*
This code produces the following output:
5 3 9 7 8 6 4 1 0
*/
Remarks
The Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) method generates a MethodCallExpression that
represents calling Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) itself as a constructed generic
method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the
IQueryProvider represented by the Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Union<TSource>
(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type of the source1
parameter. The expected behavior is that the set union of the elements in source1 and source2 is returned.
Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>,
IEqualityComparer<TSource>)
Produces the set union of two sequences by using a specified IEqualityComparer<T>.
Type Parameters
TSource
The type of the elements of the input sequences.
Parameters
source1 IQueryable<TSource>
A sequence whose distinct elements form the first set for the union operation.
source2 IEnumerable<TSource>
A sequence whose distinct elements form the second set for the union operation.
comparer IEqualityComparer<TSource>
An IEqualityComparer<T> to compare values.
Returns
IQueryable<TSource>
An IQueryable<T> that contains the elements from both input sequences, excluding duplicates.
Exceptions
ArgumentNullException
source1 or source2 is null .
Remarks
The Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method
generates a MethodCallExpression that represents calling Union<TSource>(IQueryable<TSource>,
IEnumerable<TSource>, IEqualityComparer<TSource>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the
Provider property of the source1 parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Union<TSource>
(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) depends on the implementation of the
type of the source1 parameter. The expected behavior is that the set union of the elements in source1 and source2 is
returned. The comparer parameter is used to compare values.
Queryable.Where
I n this Article
Overloads
Where<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Int32,Boolean>>) Filters a sequence of values based on a predicate. Each
element's index is used in the logic of the predicate function.
Where<TSource>(IQueryable<TSource>, Expression<Func<
TSource,Boolean>>) Filters a sequence of values based on a predicate.
Where<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>)
Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
public static System.Linq.IQueryable<TSource> Where<TSource> (this System.Linq.IQueryable<TSource>
source, System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to filter.
predicate Expression<Func<TSource,Int32,Boolean>>
A function to test each element for a condition; the second parameter of the function represents the index of the
element in the source sequence.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence that satisfy the condition specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use Where<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>) to filter a sequence based on a predicate that incorporates the index of
each element.
int[] numbers = { 0, 30, 20, 15, 90, 85, 40, 75 };
/*
This code produces the following output:
0
20
15
40
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) method generates a
MethodCallExpression that represents calling Where<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Int32,Boolean>>) itself as a constructed generic method. It then passes the
MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider
property of the source parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Where<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it returns the elements from source that satisfy the condition
specified by predicate . The index of each source element is provided as the second argument to predicate .
Where<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>)
Filters a sequence of values based on a predicate.
Type Parameters
TSource
The type of the elements of source .
Parameters
source IQueryable<TSource>
An IQueryable<T> to filter.
predicate Expression<Func<TSource,Boolean>>
A function to test each element for a condition.
Returns
IQueryable<TSource>
An IQueryable<T> that contains elements from the input sequence that satisfy the condition specified by predicate .
Exceptions
ArgumentNullException
source or predicate is null .
Examples
The following code example demonstrates how to use Where<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) to filter a sequence.
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };
/*
This code produces the following output:
apple
mango
grape
*/
Remarks
This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the
Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an
Expression<TDelegate>.
The Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) method generates a
MethodCallExpression that represents calling Where<TSource>(IQueryable<TSource>,
Expression<Func<TSource,Boolean>>) itself as a constructed generic method. It then passes the MethodCallExpression
to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source
parameter.
The query behavior that occurs as a result of executing an expression tree that represents calling Where<TSource>
(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) depends on the implementation of the type of the
source parameter. The expected behavior is that it returns the elements from source that satisfy the condition
specified by predicate .
Queryable.Zip
I n this Article
Merges two sequences by using the specified predicate function.
public static System.Linq.IQueryable<TResult> Zip<TFirst,TSecond,TResult> (this
System.Linq.IQueryable<TFirst> source1, System.Collections.Generic.IEnumerable<TSecond> source2,
System.Linq.Expressions.Expression<Func<TFirst,TSecond,TResult>> resultSelector);
Type Parameters
TFirst
The type of the elements of the first input sequence.
TSecond
The type of the elements of the second input sequence.
TResult
The type of the elements of the result sequence.
Parameters
source1 IQueryable<TFirst>
The first sequence to merge.
source2 IEnumerable<TSecond>
The second sequence to merge.
resultSelector Expression<Func<TFirst,TSecond,TResult>>
A function that specifies how to merge the elements from the two sequences.
Returns
IQueryable<TResult>
An IQueryable<T> that contains merged elements of two input sequences.
Exceptions
ArgumentNullException
source1 or source2 is null .
Examples
The following code example demonstrates how to use the Zip method to merge two sequences.
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
var numbersAndWords = numbers.AsQueryable().Zip(words, (first, second) => first + " " + second);
// 1 one
// 2 two
// 3 three
Remarks
The Zip method generates a MethodCallExpression that represents calling Zip itself as a constructed generic method. It
then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider
represented by the Provider property of the source1 parameter.
The method merges each element of the first sequence with an element that has the same index in the second
sequence. If the sequences do not have the same number of elements, the method merges sequences until it reaches
the end of one of them. For example, if one sequence has three elements and the other one has four, the resulting
sequence will have only three elements.