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
SelectMany: Flattens collections into a single collection (similar to cross join in SQL).
SelectMany: Flattens collections into a single collection (similar to cross join in SQL). |
SelectMany: Flattens collections into a single collection (similar to cross join in SQL). |
This Query Expression sample cross joins two arrays, and gets cartesian product. |
This Query Expression sample cross joins two arrays, and gets cartesian product. |
static void Sample_SelectMany_Linq() { string[] fruits = { "Grape", "Orange", "Apple" }; int[] amounts = { 1, 2, 3 }; var result = from f in fruits from a in amounts select new { Fruit = f, Amount = a }; Debug.WriteLine("Selecting all values from each array, and mixing them:"); foreach (var o in result) Debug.WriteLine(String.Format("{0}, {1}", o.Fruit, o.Amount)); }Output:
Selecting all values from each array, and mixing them: Grape, 1 Grape, 2 Grape, 3 Orange, 1 Orange, 2 Orange, 3 Apple, 1 Apple, 2 Apple, 3
Private Shared Sub Sample_SelectMany_Linq() Dim fruits As String() = {"Grape", "Orange", "Apple"} Dim amounts As Integer() = {1, 2, 3} Dim result = From f In fruits From a In amounts Select New With { _ Key .Fruit = f, _ Key .Amount = a _ } Debug.WriteLine("Selecting all values from each array, and mixing them:") For Each o In result Debug.WriteLine([String].Format("{0}, {1}", o.Fruit, o.Amount)) Next End SubOutput:
Selecting all values from each array, and mixing them: Grape, 1 Grape, 2 Grape, 3 Orange, 1 Orange, 2 Orange, 3 Apple, 1 Apple, 2 Apple, 3
static void Sample_SelectMany_Lambda() { string[] fruits = { "Grape", "Orange", "Apple" }; int[] amounts = { 1, 2, 3 }; var result = fruits.SelectMany(f => amounts, (f, a) => new { Fruit = f, Amount = a }); Debug.WriteLine("Selecting all values from each array, and mixing them:"); foreach (var o in result) Debug.WriteLine(o.Fruit + ", " + o.Amount); }Output:
Selecting all values from each array, and mixing them: Grape, 1 Grape, 2 Grape, 3 Orange, 1 Orange, 2 Orange, 3 Apple, 1 Apple, 2 Apple, 3
Private Shared Sub Sample_SelectMany_Lambda() Dim fruits As String() = {"Grape", "Orange", "Apple"} Dim amounts As Integer() = {1, 2, 3} Dim result = fruits.SelectMany(Function(f) amounts, Function(f, a) New With { _ Key .Fruit = f, _ Key .Amount = a _ }) Debug.WriteLine("Selecting all values from each array, and mixing them:") For Each o In result Debug.WriteLine([String].Format("{0}, {1}", o.Fruit, o.Amount)) Next End SubOutput:
Selecting all values from each array, and mixing them: Grape, 1 Grape, 2 Grape, 3 Orange, 1 Orange, 2 Orange, 3 Apple, 1 Apple, 2 Apple, 3