我发现了以下关于 .Select 和 .SelectMany 之间差异的示例
public class PhoneNumber
{
public string Number { get; set; }
}
public class Person
{
public IEnumerable<PhoneNumber> PhoneNumbers { get; set; }
}
IEnumerable<Person> people = new List<Person>();
// Select gets a list of lists of phone numbers
IEnumerable<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);
但我实际上无法理解两者之间有什么区别(Select 获取电话号码列表,而 SelectMany 将其展平为电话号码列表。)。
第二个问题,写作之间有什么区别:-
people.Select(p => p.PhoneNumbers);
&
people.Include(p => p.PhoneNumbers);