Aggregation
Aggregate, Average, Count, LongCount, Max, Min, Sum
Conversion
AsEnumerable, Cast, OfType, ToArray, ToDictionary, 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
Grouping
GroupBy
Join
GroupJoin, Join
Ordering
OrderBy, OrderByDescending, Reverse, ThenBy, ThenByDescending
OrderBy (simple - numbers)
OrderBy (simple - dates)
OrderBy (simple - objects)
OrderByDescending
Reverse
ThenBy
ThenByDescending
Other
Concat, SequenceEqual, Zip
Partitioning
Skip, SkipWhile, Take, TakeWhile
Projection
Select, SelectMany
Quantifiers
All, Any, Contains
Restriction
Where
Set
Distinct, Except, Intersect, Union
Close
GroupBy: Projects elements of a collection into groups by key.
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. |
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
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 SubOutput:
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