Reputation: 729
I'm a C# novice. Can someone help me understanding this C# lambda expression?
var projs = allCustomers.SelectMany(osd => osd.phoneNumbers,
(osd, osv) => new { customer= osd, phoneNumber= osv });
Thanks/Hem
Upvotes: 0
Views: 110
Reputation: 101680
You are using this overload of SelectMany
.
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector
)
There are three parameters:
source :
Type: System.Collections.Generic.IEnumerable<TSource>
A sequence of values to project.
collectionSelector
Type: System.Func<TSource, IEnumerable<TCollection>>
A transform function to apply to each element of the input sequence.
resultSelector
Type: System.Func<TSource, TCollection, TResult>
A transform function to apply to each element of the intermediate sequence.
In your case, source
is the allCustomers
, collectionSelector
is the expression:
osd => osd.phoneNumber
and the resultSelector
is:
(osd, osv) => new { customer= osd, phoneNumber= osv }
Here the first expression says take each customer
and return it's phoneNumbers
.In the second expression, the type of osd
is customer
, osv
is phoneNumber
and the result is an anonymous type.It takes each customer and the phone number and creates an anonymous type using these values.
Here is an example of what this query does:
Customer - Phone Numbers
------------------------
John 1234567,2331212,1122334
Jack 1456771,9485323
Juliet 2401232
The result would be:
John 1234567
John 2331212
John 1122334
Jack 1456771
Jack 9485323
Juliet 2401232
Upvotes: 9