Projection refers to the operations of transforming an object into a new form.
Select
Projects values that are based on a transform function.
Method Syntax
// Select
var numbers = new int[] { 1, 2, 3, 4, 5 };
var strings = numbers.Select(n => n.ToString());
// strings = { "1", "2", "3", "4", "5" }
Query Syntax
// select
var numbers = new int[] { 1, 2, 3, 4, 5 };
var strings = from n in numbers
select n.ToString();
// strings = { "1", "2", "3", "4", "5" }
SelectMany
Projects sequences of values that are based on a transform function and then flattens them into one sequence.
Method Syntax
// SelectMany
class Customer
{
public Order[] Orders { get; set; }
}
class Order
{
public Order(string desc) { Description = desc; }
public string Description { get; set; }
}
...
var customers = new Customer[]
{
new Customer { Orders = new Order[] { new Order("O1"), new Order("O2") } },
new Customer { Orders = new Order[] { new Order("O3") } },
new Customer { Orders = new Order[] { new Order("O4") } },
};
var orders = customers.SelectMany(c => c.Orders);
// orders = { Order("O1"), Order("O3"), Order("O3"), Order("O4") }
Query Syntax
// multiples from
var orders = from c in customers
from o in c.Orders
select o;
// orders = { Order("O1"), Order("O3"), Order("O3"), Order("O4") }