Introduction
LINQ (Language Integrated Query) is an expression that retrieves data from a data source. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats. In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a provider is available. LINQ can be used in C# and VB.
Syntax
- public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
- public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
- public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
- public static Boolean All<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static Boolean Any<TSource>(this IEnumerable<TSource> source)
- public static Boolean Any<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
- public static Decimal Average(this IEnumerable<Decimal> source)
- public static Double Average(this IEnumerable<Double> source)
- public static Double Average(this IEnumerable<Int32> source)
- public static Double Average(this IEnumerable<Int64> source)
- public static Nullable<Decimal> Average(this IEnumerable<Nullable<Decimal>> source)
- public static Nullable<Double> Average(this IEnumerable<Nullable<Double>> source)
- public static Nullable<Double> Average(this IEnumerable<Nullable<Int32>> source)
- public static Nullable<Double> Average(this IEnumerable<Nullable<Int64>> source)
- public static Nullable<Single> Average(this IEnumerable<Nullable<Single>> source)
- public static Single Average(this IEnumerable<Single> source)
- public static Decimal Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Decimal> selector)
- public static Double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Double> selector)
- public static Double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32> selector)
- public static Double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Int64> selector)
- public static Nullable<Decimal> Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector)
- public static Nullable<Double> Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Double>> selector)
- public static Nullable<Double> Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector)
- public static Nullable<Double> Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector)
- public static Nullable<Single> Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector)
- public static Single Average<TSource>(this IEnumerable<TSource> source, Func<TSource, Single> selector)
- public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
- public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
- public static Boolean Contains<TSource>(this IEnumerable<TSource> source, TSource value)
- public static Boolean Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
- public static Int32 Count<TSource>(this IEnumerable<TSource> source)
- public static Int32 Count<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable<TSource> source)
- public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable<TSource> source, TSource defaultValue)
- public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source)
- public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
- public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, Int32 index)
- public static TSource ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, Int32 index)
- public static IEnumerable<TResult> Empty<TResult>()
- public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
- public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
- public static TSource First<TSource>(this IEnumerable<TSource> source)
- public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source)
- public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
- public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
- public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
- public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
- public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
- public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
- public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)
- public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- public static TSource Last<TSource>(this IEnumerable<TSource> source)
- public static TSource Last<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source)
- public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static Int64 LongCount<TSource>(this IEnumerable<TSource> source)
- public static Int64 LongCount<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static Decimal Max(this IEnumerable<Decimal> source)
- public static Double Max(this IEnumerable<Double> source)
- public static Int32 Max(this IEnumerable<Int32> source)
- public static Int64 Max(this IEnumerable<Int64> source)
- public static Nullable<Decimal> Max(this IEnumerable<Nullable<Decimal>> source)
- public static Nullable<Double> Max(this IEnumerable<Nullable<Double>> source)
- public static Nullable<Int32> Max(this IEnumerable<Nullable<Int32>> source)
- public static Nullable<Int64> Max(this IEnumerable<Nullable<Int64>> source)
- public static Nullable<Single> Max(this IEnumerable<Nullable<Single>> source)
- public static Single Max(this IEnumerable<Single> source)
- public static TSource Max<TSource>(this IEnumerable<TSource> source)
- public static Decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Decimal> selector)
- public static Double Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Double> selector)
- public static Int32 Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32> selector)
- public static Int64 Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Int64> selector)
- public static Nullable<Decimal> Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector)
- public static Nullable<Double> Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Double>> selector)
- public static Nullable<Int32> Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector)
- public static Nullable<Int64> Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector)
- public static Nullable<Single> Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector)
- public static Single Max<TSource>(this IEnumerable<TSource> source, Func<TSource, Single> selector)
- public static TResult Max<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
- public static Decimal Min(this IEnumerable<Decimal> source)
- public static Double Min(this IEnumerable<Double> source)
- public static Int32 Min(this IEnumerable<Int32> source)
- public static Int64 Min(this IEnumerable<Int64> source)
- public static Nullable<Decimal> Min(this IEnumerable<Nullable<Decimal>> source)
- public static Nullable<Double> Min(this IEnumerable<Nullable<Double>> source)
- public static Nullable<Int32> Min(this IEnumerable<Nullable<Int32>> source)
- public static Nullable<Int64> Min(this IEnumerable<Nullable<Int64>> source)
- public static Nullable<Single> Min(this IEnumerable<Nullable<Single>> source)
- public static Single Min(this IEnumerable<Single> source)
- public static TSource Min<TSource>(this IEnumerable<TSource> source)
- public static Decimal Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Decimal> selector)
- public static Double Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Double> selector)
- public static Int32 Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32> selector)
- public static Int64 Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Int64> selector)
- public static Nullable<Decimal> Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector)
- public static Nullable<Double> Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Double>> selector)
- public static Nullable<Int32> Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector)
- public static Nullable<Int64> Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector)
- public static Nullable<Single> Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector)
- public static Single Min<TSource>(this IEnumerable<TSource> source, Func<TSource, Single> selector)
- public static TResult Min<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
- public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source)
- public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- public static IEnumerable<Int32> Range(Int32 start, Int32 count)
- public static IEnumerable<TResult> Repeat<TResult>(TResult element, Int32 count)
- public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source)
- public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
- public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Int32, TResult> selector)
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
- public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, Int32, IEnumerable<TResult>> selector)
- public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
- public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, Int32, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
- public static Boolean SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
- public static Boolean SequenceEqual<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
- public static TSource Single<TSource>(this IEnumerable<TSource> source)
- public static TSource Single<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source)
- public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<TSource> Skip<TSource>(this IEnumerable<TSource> source, Int32 count)
- public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<TSource> SkipWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32, Boolean> predicate)
- public static Decimal Sum(this IEnumerable<Decimal> source)
- public static Double Sum(this IEnumerable<Double> source)
- public static Int32 Sum(this IEnumerable<Int32> source)
- public static Int64 Sum(this IEnumerable<Int64> source)
- public static Nullable<Decimal> Sum(this IEnumerable<Nullable<Decimal>> source)
- public static Nullable<Double> Sum(this IEnumerable<Nullable<Double>> source)
- public static Nullable<Int32> Sum(this IEnumerable<Nullable<Int32>> source)
- public static Nullable<Int64> Sum(this IEnumerable<Nullable<Int64>> source)
- public static Nullable<Single> Sum(this IEnumerable<Nullable<Single>> source)
- public static Single Sum(this IEnumerable<Single> source)
- public static Decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Decimal> selector)
- public static Double Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Double> selector)
- public static Int32 Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32> selector)
- public static Int64 Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Int64> selector)
- public static Nullable<Decimal> Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Decimal>> selector)
- public static Nullable<Double> Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Double>> selector)
- public static Nullable<Int32> Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int32>> selector)
- public static Nullable<Int64> Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Int64>> selector)
- public static Nullable<Single> Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Nullable<Single>> selector)
- public static Single Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, Single> selector)
- public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, Int32 count)
- public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32, Boolean> predicate)
- public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
- public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)
- public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
- public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
- public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
- public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
- public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- public static ILookup<TKey, TSource> ToLookup<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
- public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
- public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
- public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
- public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
- public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, Boolean> predicate)
- public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, Int32, Boolean> predicate)
- public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
The LINQ built-in methods are extension methods for the IEnumerable<T>
interface that live in the System.Linq.Enumerable
class in the System.Core
assembly. They are available in .NET Framework 3.5 and later.
LINQ allows for simple modification, transformation, and combination of various IEnumerable
s using a query-like or functional syntax.
While the standard LINQ methods can work on any IEnumerable<T>
, including the simple arrays and List<T>
s, they can also be used on database objects, where the set of LINQ expressions can be transformed in many cases to SQL if the data object supports it. See LINQ to SQL.
For the methods that compare objects (such as Contains
and Except
), IEquatable<T>.Equals
is used if the type T of the collection implements that interface. Otherwise, the standard Equals
and GetHashCode
of the type (possibly overriden from the default Object
implementations) are used. There are also overloads for these methods that allow to specify a custom IEqualityComparer<T>
.
For the ...OrDefault
methods, default(T)
is used to generate default values.
Official reference: Enumerable class
Lazy Evaluation
Virtually every query that returns an IEnumerable<T>
is not evaluated immediately; instead, the logic is delayed until the query is iterated over. One implication is that each time someone iterates over an IEnumerable<T>
created from one of these queries, e.g., .Where()
, the full query logic is repeated. If the predicate is long-running, this can be a cause for performance issues.
One simple solution (when you know or can control the approximate size of the resulting sequence) is to fully buffer the results using .ToArray()
or .ToList()
. .ToDictionary()
or .ToLookup()
can fulfill the same role. One can also, of course, iterate over the entire sequence and buffer the elements according to other custom logic.
ToArray()
or ToList()
?
Both .ToArray()
and .ToList()
loop through all elements of an IEnumerable<T>
sequence and save the results in a collection stored in-memory. Use the following guidelines to determine which to choose:
- Some APIs may require a
T[]
or a List<T>
.
.ToList()
typically runs faster and generates less garbage than .ToArray()
, because the latter must copy all the elements into a new fixed-size collection one more time than the former, in almost every case.
- Elements can be added to or removed from the
List<T>
returned by .ToList()
, whereas the T[]
returned from .ToArray()
remains a fixed size throughout its lifetime. In other words, List<T>
is mutable, and T[]
is immutable.
- The
T[]
returned from.ToArray()
uses less memory than the List<T>
returned from .ToList()
, so if the result is going to be stored for a long time, prefer .ToArray()
. Calling List<T>.TrimExcess()
would make the memory difference strictly academic, at the cost of eliminating the relative speed advantage of .ToList()
.