1.

What is Select() and SelectMany() in LINQ?

Answer»

In LINQ, Select() and SelectMany() are projection OPERATORS. The use of a Select() OPERATOR is to select a value from a collection whereas the use of SelectMany() operator is to select values from a GROUP of collection, i.e. a nested collection.

Example

public class PhoneNumber
{
    public string Number { GET; set; }
}
public class Person
{
   public IEnumerable PhoneNumbers { get; set; }
   public string Name { get; set; }
}
IEnumerable PEOPLE = new List();
IEnumerable> phoneLists = people.Select(p => p.PhoneNumbers);
IEnumerable phoneNumbers = people.SelectMany(p => p.PhoneNumbers);
var directory = people .SelectMany(p => p.PhoneNumbers,
(parent, child) => new { parent.Name, child.Number });



Discussion

No Comment Found