{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The GroupBy operator groups the elements of a sequence.
EditMethod Signatures
// 1 - Group the source sequence by the specified key selector function.
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
// 2 - Group the source sequence by the specified key selector function.
// Use a custom equality comparer for determining groups.
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer)
// 3 - Group the source sequence by the specified key selector function.
// Project results using the element selector function
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> 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<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TElement> elementSelector,
IEqualityComparer<TKey> comparer)
EditExceptions
Throws an ArgumentNullException if
source,
keySelector, or
elementSelector is null.
EditPseudo-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).
EditLoop 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.
EditCode Samples
EditC# 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=
=
EditVB 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 SubResult 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 }