LINQSamples.com
  • Samples
  • Tutorials
  • About
  • Links
  • Contact
LINQ to Objects

Aggregation

Aggregate, Average, Count, LongCount, Max, Min, Sum

      Aggregate (simple)       Aggregate (seed)       Average       Count       LongCount       Max       Min       Sum

Conversion

AsEnumerable, Cast, OfType, ToArray, ToDictionary, ToList, ToLookup

      AsEnumerable       Cast       OfType       ToArray       ToDictionary (simple)       ToDictionary (conditional)       ToList       ToLookup

Element

ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault

      ElementAt       ElementAtOrDefault       First (simple)       First (conditional)       FirstOrDefault       Last       LastOrDefault (simple)       LastOrDefault (conditional)       Single       SingleOrDefault

Generation

DefaultIfEmpty, Empty, Range, Repeat

      DefaultIfEmpty (simple)       DefaultIfEmpty (default value)       Empty       Range       Repeat

Grouping

GroupBy

      GroupBy

Join

GroupJoin, Join

      GroupJoin (left outer join)       Join (inner join)

Ordering

OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending

      OrderBy (simple - numbers)       OrderBy (simple - dates)       OrderBy (simple - objects)       OrderByDescending       Reverse       ThenBy       ThenByDescending

Other

Concat, SequenceEqual, Zip

      Concat (simple - numbers)       Concat (simple - strings)       SequenceEqual       Zip

Partitioning

Skip, SkipWhile, Take, TakeWhile

      Skip       SkipWhile       Take       TakeWhile

Projection

Select, SelectMany

      Select (simple)       Select (anonymous type)       Select (indexed)       SelectMany (cross join)

Quantifiers

All, Any, Contains

      All       Any       Contains

Restriction

Where

      Where (simple - numbers)       Where (simple - objects)       Where (indexed)

Set

Distinct, Except, Intersect, Union

      Distinct       Except       Intersect       Union
  Close

ToLookup

Lambda Query

ToLookup: Converts a collection into a Lookup, grouped by key.

ToLookup: Converts a collection into a Lookup, grouped by key.

This Lambda Expression sample puts array elements into a one-to-many Lookup, where key equals element length.

This Lambda Expression sample puts array elements into a one-to-many Lookup, where key equals element length.

  • C#
  • VB.Net
  • F#
// A Query Expression cannot be constructed for ToLookup() in C#.
// Consider using a Lambda Expression instead.
' A Query Expression cannot be constructed for ToLookup() in VB.NET.
' Consider using a Lambda Expression instead.
// A Query Expression cannot be constructed for ToLookup() in F#.
// Consider using a Lambda Expression instead.
  • C#
  • VB.Net
  • F#
static void Sample_ToLookup_Lambda()
{
    string[] words = { "one", "two", "three", "four", "five", "six", "seven" };

    var result = words.ToLookup(w => w.Length);

    for (int i = 1; i <= 5; i++)
    {
        Debug.WriteLine(String.Format("Elements with a length of {0}:", i));
        foreach (string word in result[i])
            Debug.WriteLine(word);
    }
}
Output:
Elements with a length of 1:
Elements with a length of 2:
Elements with a length of 3:
one
two
six
Elements with a length of 4:
four
five
Elements with a length of 5:
three
seven
Private Shared Sub Sample_ToLookup_Lambda()
    Dim words As String() = {"one", "two", "three", "four", "five", "six", _
        "seven"}

    Dim result = words.ToLookup(Function(w) w.Length)

    For i As Integer = 1 To 5
        Debug.WriteLine([String].Format("Elements with a length of {0}:", i))
        For Each word As String In result(i)
            Debug.WriteLine(word)
        Next
    Next
End Sub
Output:
Elements with a length of 1:
Elements with a length of 2:
Elements with a length of 3:
one
two
six
Elements with a length of 4:
four
five
Elements with a length of 5:
three
seven
let Sample_ToLookup_Lambda() =
    let words = [|"one"; "two"; "three"; "four"; "five"; "six"; "seven"|]

    let result = words.ToLookup(fun w -> w.Length)

    for i in 1 .. 5 do
        Debug.WriteLine(sprintf "Elements with a length of %d:" i)
        for word in result.[i] do
            Debug.WriteLine(sprintf "%s" word)
Output:
Elements with a length of 1:
Elements with a length of 2:
Elements with a length of 3:
one
two
six
Elements with a length of 4:
four
five
Elements with a length of 5:
three
seven

Share this sample on:

© 2023 - LINQSamples.com | Terms Of Service  -  Privacy Policy