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

GroupBy

Lambda Query

GroupBy: Projects elements of a collection into groups by key.

GroupBy: Projects elements of a collection into groups by key.

This Lambda Expression sample splits array of numbers into two groups: one which is divisible by 10, and one which is not.

This Lambda Expression sample splits array of numbers into two groups: one which is divisible by 10, and one which is not.

  • C#
static void Sample_GroupBy_Linq()
{
    int[] numbers = { 10, 15, 20, 25, 30, 35 };

    var result = from n in numbers
                 group n by (n % 10 == 0) into groups
                 select groups;

    Debug.WriteLine("GroupBy has created two groups:");
    foreach (IGrouping<bool, int> group in result)
    {
        if (group.Key == true)
            Debug.WriteLine("Divisible by 10");
        else
            Debug.WriteLine("Not Divisible by 10");

        foreach (int number in group)
            Debug.WriteLine(number);
    }
}
Output:
GroupBy has created two groups:
Divisible by 10
10
20
30
Not Divisible by 10
15
25
35
  • C#
  • VB.Net
  • F#
static void Sample_GroupBy_Lambda()
{
    int[] numbers = { 10, 15, 20, 25, 30, 35 };

    var result = numbers.GroupBy(n => (n % 10 == 0));

    Debug.WriteLine("GroupBy has created two groups:");
    foreach (IGrouping<bool, int> group in result)
    {
        if (group.Key == true)
            Debug.WriteLine("Divisible by 10");
        else
            Debug.WriteLine("Not Divisible by 10");

        foreach (int number in group)
            Debug.WriteLine(number);
    }
}
Output:
GroupBy has created two groups:
Divisible by 10
10
20
30
Not Divisible by 10
15
25
35
Private Shared Sub Sample_GroupBy_Lambda()
    Dim numbers As Integer() = {10, 15, 20, 25, 30, 35}

    Dim result = numbers.GroupBy(Function(n) (n Mod 10 = 0))

    Debug.WriteLine("GroupBy has created two groups:")
    For Each group As IGrouping(Of Boolean, Integer) In result
        If group.Key = True Then
            Debug.WriteLine("Divisible by 10")
        Else
            Debug.WriteLine("Not Divisible by 10")
        End If

        For Each number As Integer In group
            Debug.WriteLine(number)
        Next
    Next
End Sub
Output:
GroupBy has created two groups:
Divisible by 10
10
20
30
Not Divisible by 10
15
25
35
let Sample_GroupBy_Lambda() =
    let numbers = [|10; 15; 20; 25; 30; 35|]

    let result = numbers.GroupBy(fun n -> (n % 10 = 0));

    Debug.WriteLine(sprintf "GroupBy has created two groups:")

    for group in result do
        if group.Key = true
        then Debug.WriteLine(sprintf "Divisible by 10")
        else Debug.WriteLine(sprintf "Not divisible by 10")

        for number in group do
            Debug.WriteLine(sprintf "%d" number)
Output:
Divisible by 10
10
20
30
Not divisible by 10
15
25
35

Share this sample on:

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