我是C#新手.有人可以帮我理解这个C#lambda表达式吗?
var projs = allCustomers.SelectMany(osd => osd.phoneNumbers,
(osd, osv) => new { customer= osd, phoneNumber= osv });
Run Code Online (Sandbox Code Playgroud)
谢谢/下摆
您正在使用此重载的SelectMany.
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector
)
Run Code Online (Sandbox Code Playgroud)
有三个参数:
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.
Run Code Online (Sandbox Code Playgroud)
在你的情况下,source是allCustomers,collectionSelector是表达式:
osd => osd.phoneNumber
Run Code Online (Sandbox Code Playgroud)
而且resultSelector是:
(osd, osv) => new { customer= osd, phoneNumber= osv }
Run Code Online (Sandbox Code Playgroud)
这里第一个表达式表示取每个customer并返回它phoneNumbers.在第二个表达式中,osdis customer,osvis 的类型phoneNumber和结果是匿名类型.它接受每个客户和电话号码并使用这些值创建匿名类型.
以下是此查询的作用示例:
Customer - Phone Numbers
------------------------
John 1234567,2331212,1122334
Jack 1456771,9485323
Juliet 2401232
Run Code Online (Sandbox Code Playgroud)
结果将是:
John 1234567
John 2331212
John 1122334
Jack 1456771
Jack 9485323
Juliet 2401232
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |