.Select Vs .Include Vs .Select Many

c#, entity-framework, linq

Solution

Select gets a list of lists of phone numbers , while, SelectMany flattens it to just a list of phone numbers.

That's exactly the difference. `SelectMany` takes a collection of collections and return all items as one collection.

people.Select(p => p.PhoneNumbers);

Select only the phone numbers.

people.Include(p => p.PhoneNumbers); 

Select people entities with phone numbers loaded.

Problem

I found the following example regarding the differences between .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); ``` But i can not actually understnad exaclty what is the difference between (Select gets a list of lists of phone numbers , while, SelectMany flattens it to just a list of phone numbers.). Seond question , what is the difference between writing:- ``` people.Select(p => p.PhoneNumbers); ``` & ``` people.Include(p => p.PhoneNumbers); ```

Original source