What is the difference between Select and SelectMany?

Technology CommunityCategory: LINQWhat is the difference between Select and SelectMany?
VietMX Staff asked 3 years ago
  • Select is a simple one-to-one projection from source element to a result element.
  • SelectMany is used when there are multiple from clauses in a query expression: each element in the original sequence is used to generate a new sequence. It also flattens queries that return lists of lists.

Consider:

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 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);

// 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 });