Code like song
I thought I’d review the special methods available in Linq. In the API reference, they can be found under the Enumerable<T>
class. Here they are:
static IEnumerable<T> Empty<T>()
static IEnumerable<T> Repeat<T>(T element, int count)
T[] ToArray()
List<T> ToList()
Dictionary<K,T> ToDictionary(Func<T,K> keySelector) // puts every element in a dictionary
// using keys generated by keySelector
Dictionary<K,T> ToDictionary(Func<T,K> keySelector, IEqualityComparer<K>)
Dictionary<K,R> ToDictionary(Func<T,K> keySelector, Func<T,R> elemSelector)
// transforms each T to an R and puts the Rs in the dictionary
Dictionary<K,R> ToDictionary(Func<T,K> keySelector, Func<T,R> elemSelector, IEqualityComparer<K>)
Lookup<K,T> ToLookup(Func<T,K> keySelector) // a Lookup is like a Dictionary except
// each key points to a list of values.
// In Perl terms it's a hash of arrays.
Lookup<K,T> ToLookup(Func<T,K> keySelector, IEqualityComparer<K>)
Lookup<K,R> ToLookup(Func<T,K> keySelector, Func<T,R> elemSelector)
Lookup<K,R> ToLookup(Func<T,K> keySelector, Func<T,R> elemSelector, IEqualityComparer<K>)
T Aggregate(Func<T,T,T> adder) // like python's reduce
T ElementAt(int index)
T ElementAtOrDefault(int index) // returns this[i] or default(T) if i >= this.Count
T First()
T First(Func<T,Boolean>) // the first element that satisfies the predicate
T FirstOrDefault()
T FirstOrDefault(Func<T,Boolean>)
T Last()
T Last(Func<T,Boolean>) // the last element that satisfies the predicate
T LastOrDefault()
T LastOrDefault(Func<T,Boolean>)
T Single() // returns the only element of the sequence,
// or throws an exception
T Single(Func<T,Boolean> f) // returns the only element for which f returns true.
// Exception if there is not one and only one.
T SingleOrDefault() // default(T) if empty. Still exception if more than one T.
T SingleOrDefault(Func<T,Boolean> f)
bool Contains(T)
bool Contains(T, IEqualityComparer<T>)
bool All(Func<T, bool> predicate) // true if predicate is always true
bool Any(Func<T, bool> predicate) // true if predicate is ever true
bool Any() // true if the sequence contains any elements
bool SequenceEqual(IEnumerable<T>) // true if all the elements of the two sequences
// are equal, using T's default equality comparer.
bool SequenceEqual(IEnumerable<T>, IEqualityComparer<T>)
int Count()
int Count(Func<T,Boolean>) // counts how many elements satisfy the predicate.
long LongCount()
long LongCount(Func<T,Boolean>)
int Average()
int Average(Func<T,int> transform) // calls transform to alter each element before
// including it in the average.
// You could use this to get a weighted average, e.g.
int Max()
int Max(Func<T,int> transform) // uses transform to turn each T into an int
int Min()
int Min(Func<T,int> transform)
int Sum()
int Sum(Func<T,int> transform)
// ... lots of overloads ...
IEnumerable<T> DefaultIfEmpty() // returns [default(T)] if empty
IEnumerable<T> DefaultIfEmpty(T elem) // returns [elem] if empty.
IEnumerable<T> AsEnumerable() // forces the compiler to call IEnumerable<T> methods
IEnumerable<R> Cast<R>() // casts each element to an R
IEnumerable<R> OfType<R>() // filters out any elements not of type R
IEnumerable<T> Intersect(IEnumerable<T> compare) // the standard set operation
IEnumerable<T> Intersect(IEnumerable<T> compare, IEqualityComparer<T>)
IEnumerable<T> Union(IEnumerable<T> compare) // the standard set operation
IEnumerable<T> Union(IEnumerable<T> compare, IEqualityComparer<T>)
IEnumerable<T> Except(IEnumerable<T> compare) // returns this - compare
IEnumerable<T> Except(IEnumerable<T> compare, IEqualiterComperer<T>)
IEnumerable<T> Distinct() // like SQL distinct keyword
IEnumerable<T> Distinct(IEqualityComperer<T>)
IEnumerable<T> Concat(IEnumerable<T>) // like Union, but with list semantics
// instead of set
IEnumerable<T> Range(int start, int count)
IEnumerable<T> Skip(int n) // skips n elements; returns the rest
IEnumerable<T> SkipWhile(Func<T,Boolean> f) // skips while f is true; returns the rest
IEnumerable<T> SkipWhile(Func<T,int,Boolean> f) // f also gets the 0-based index
IEnumerable<T> Take(int n) // returns Range(0,n)
IEnumerable<T> TakeWhile(Func<T,Boolean> f) // takes while f is true; skips the rest
IEnumerable<T> TakeWhile(Func<T,int,Boolean> f)
IEnumerable<T> While(Func<T,Boolean> f) // gets the elements where f is true.
IEnumerable<T> While(Func<T,int,Boolean> f) // f also knows the 0-based index.
IEnumerable<IGrouping<K,T>> GroupBy(Func<T,K> f) // f generates a key for each element
IEnumerable<IGrouping<K,R>> GroupBy(Func<T,K> f, Func<K, IEnumerable<T>, R> resultSelector)
// resultSelector gets an enumeration of values with a given key,
// and it returns a single value of type R, e.g. representing
// their min value. Note that you could use anonymous classes
// to stick several values into R, e.g. a min, max, and average.
// There is a nice example here:
// [http://msdn.microsoft.com/en-us/library/bb549393.aspx](http://msdn.microsoft.com/en-us/library/bb549393.aspx).
... lots more overloads ...
IOrderedEnumerable<T> OrderBy(Func<T,K> keySelector) // orders the elements based on keys
// generated by keySelector
IOrderedEnumerable<T> OrderBy(Func<T,K> keySelector, IComparer<K> comp)
// orders the elements based on keys generated by keySelector
// and compared by comp
IOrderedEnumerable<T> OrderByDescending(Func<T,K> keySelector)
IOrderedEnumerable<T> OrderByDescending(Func<T,K> keySelector, IComparer<K> comp)
IEnumerable<T> Reverse()
IOrderedEnumerable<T> ThenBy(Func<T,K> keySelector) // sub-sorts elements deemed equal
// by a previous sort
IOrderedEnumerable<T> ThenBy(Func<T,K> keySelector, IComparer<K> comp)
IOrderedEnumerable<T> ThenByDescending(Func<T,K> keySelector)
IOrderedEnumerable<T> ThenByDescending(Func<T,K> keySelector, IComparer<K> comp)
IEnumerable<T> Join(IEnumerable<I> inner, Func<O,K> outerKeySelector,
Func<I,K> innerKeySelector, Func<O,I,R> resultSelector)
// joins two sequences as an inner equijoin.
// There is no method for outer joins,
// but GroupJoin can get used to get that effect.
IEnumerable<O,I,K,R> GroupJoin(IEnumerable<I> inner, Func<O,K> outerKeySelector,
Func<I,K> innerKeySelector, Func<O, IEnumerable<I>, R> resultSelector)
// joins the two sequences (see Join) and groups the results (see GroupBy).
// Unlike with Join, this function can be used to do a left outer join,
// because even when there is no value in inner matching a value in outer,
// the resultSelector gets called for each outer value,
// just with an empty IEnuemrable<I>.
IEnumerable<R> Select(Func<T,R> f) // uses f to transform each T to an R
IEnumerable<R> Select(Func<T,int,R> f) // f also knows the 0-based index of each T
IEnumerable<R> SelectMany(Func<T,IEnumerable<R> f) // f can return many Rs. All the Rs
// are flattened into one sequence.
IEnumerable<R> SelectMany(Func<T,int,IEnumerable<R> f)
IEnumerable<R> SelectMany(Func<T,IEnumerable<U> f, Func<T,U,R> g)
// f generates an intermediate value of type U; g uses both a T and U
// to get an R. This is really only useful if U doesn't have access to T.
// See the example code here:
// [http://msdn.microsoft.com/en-us/library/bb534631.aspx](http://msdn.microsoft.com/en-us/library/bb534631.aspx).
IEnumerable<R> SelectMany(Func<T,int,IEnumerable<U> f, Func<T,U,R> g)