Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Companion book for this site
LINQ to Objects Using C# 4.0:
Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Quick Search

Advanced Search »

GroupBy Operator

Modified: 2011/12/02 22:52 by admin - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The GroupBy operator groups the elements of a sequence.

Edit

Method Signatures

// 1 - Group the source sequence by the specified key selector function. public static IEnumerable> GroupBy( this IEnumerable source, Func keySelector)

// 2 - Group the source sequence by the specified key selector function. // Use a custom equality comparer for determining groups. public static IEnumerable> GroupBy( this IEnumerable source, Func keySelector, IEqualityComparer comparer)

// 3 - Group the source sequence by the specified key selector function. // Project results using the element selector function public static IEnumerable> GroupBy( this IEnumerable source, Func keySelector, Func elementSelector)

// 4 - Group the source sequence by the specified key selector function. // Project results using the element selector function // Use a custom equality comparer for determining groups. public static IEnumerable> GroupBy( this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer)

Edit

Exceptions

Throws an ArgumentNullException if source, keySelector, or elementSelector is null.

Edit

Pseudo-code

Overload 1 - Group elements of the TSource, default comparer
If source is null, throw an ArgumentNullException.
If keySelector is null, throw an ArgumentNullException.
Iterate the source sequence.
Find the key value returned by keySelector(this element).
If this key does not exist in the grouping collection (as determined by using EqualityComparer<TSource>.Default)
Create a grouping of TSource elements for this key.
Iterate the groupings.
Return the current grouping. (Resume execution from here when the next element is requested).

Overload 2 - Group elements of the TSource, custom comparer
If source is null, throw an ArgumentNullException.
If keySelector is null, throw an ArgumentNullException.
Iterate the source sequence.
Find the key value returned by keySelector(this element).
If this key does not exist in the grouping collection (as determined by using the custom comparer - comparer),
Create a grouping of TSource elements for this key.
Iterate the groupings.
Return the current grouping. (Resume execution from here when the next element is requested).

Overload 3 - Group elements of the elementSelector, default comparer
If source is null, throw an ArgumentNullException.
If keySelector is null, throw an ArgumentNullException.
If elementSelector is null, throw an ArgumentNullException.
Iterate the source sequence.
Find the key value returned by keySelector(this element).
If this key does not exist in the grouping collection (as determined by using EqualityComparer<TSource>.Default)
Create a grouping of elementSelector(element) elements for this key.
Iterate the groupings.
Return the current grouping. (Resume execution from here when the next element is requested).

Overload 4 - Group elements of the elementSelector, custom comparer
If source is null, throw an ArgumentNullException.
If keySelector is null, throw an ArgumentNullException.
If elementSelector is null, throw an ArgumentNullException.
Iterate the source sequence.
Find the key value returned by keySelector(this element).
If this key does not exist in the grouping collection (as determined by using the custom comparer - comparer),
Create a grouping of elementSelector(element) elements for this key.
Iterate the groupings.
Return the current grouping. (Resume execution from here when the next element is requested).

Edit

Loop Count

1. One full loop through the source sequence to build groups. This operator implements the standard deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.


Edit

Code Samples

Edit

C# example using Group By

var processInfoByThreadCount = from process in Process.GetProcesses() group process by process.Threads.Count into g orderby g.Key descending select new { ThreadCount = g.Key, ProcessCount = g.Count(), ThreadSum = g.Sum( p => p.Threads.Count ) }; foreach (var item in processInfoByThreadCount) { Debug.WriteLine(item); } Edit

=

=
Edit

VB sample uses Group By to group process by threads count

Public Sub Linq()

Dim query = From process In System.Diagnostics.Process.GetProcesses() _ Group By ThreadCount = process.Threads.Count _ Into ProcCount = Count(), ThreadSum = Sum(process.Threads.Count) _ Select ProcCount, ThreadCount, ThreadSum _ Order By ProcCount Descending, ThreadCount

Console.WriteLine("Result:") For Each c In query Console.WriteLine(c) Next End Sub

Result may vary,
Result:
{ ProcCount = 8, ThreadCount = 3, ThreadSum = 24 }
{ ProcCount = 5, ThreadCount = 11, ThreadSum = 55 }
{ ProcCount = 4, ThreadCount = 1, ThreadSum = 4 }
{ ProcCount = 4, ThreadCount = 4, ThreadSum = 16 }
{ ProcCount = 4, ThreadCount = 5, ThreadSum = 20 }
{ ProcCount = 4, ThreadCount = 7, ThreadSum = 28 }
{ ProcCount = 4, ThreadCount = 13, ThreadSum = 52 }
{ ProcCount = 3, ThreadCount = 6, ThreadSum = 18 }
{ ProcCount = 3, ThreadCount = 8, ThreadSum = 24 }
{ ProcCount = 3, ThreadCount = 10, ThreadSum = 30 }
{ ProcCount = 3, ThreadCount = 16, ThreadSum = 48 }
{ ProcCount = 2, ThreadCount = 9, ThreadSum = 18 }

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Other websites by this author: Focused Objective, Geek Speak Decoded.