SelectMany展平返回列表列表的查询。例如
public class PhoneNumber{ public string Number { get; set; }}public class Person{ public IEnumerable<PhoneNumber> PhoneNumbers { get; set; } public string Name { get; set; }}IEnumerable<Person> people = new List<Person>();// Select gets a list of lists of phone numbersIEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers);// SelectMany flattens it to just a list of phone numbers.IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers);// And to include data from the parent in the result: // pass an expression to the second parameter (resultSelector) in the overload:var directory = people .SelectMany(p => p.PhoneNumbers, (parent, child) => new { parent.Name, child.Number });
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)