The projection operators??”Select and SelectMany??”select values given the appropriate function.
While both select values, the SelectMany operator can handle multiple collections.
55
Part I: Introduction to Project LINQ
Select
The Select operator (select in C#) projects values from a single sequence or collection. The following
example uses select to return the FirstName, LastName, and EmailAddress columns from the sequence:
var query =
from c in contact
where c.FirstName.StartsWith("S")
select new {c.FirstName, c.LastName, c.EmailAddress}
This operator returns an enumerable object. When the object is enumerated, it produces each element in
the selected results.
This same query can be written using method syntax as follows:
var query =
contact.Select(c => new {
c.FirstName, c.Lastname, c.EmailAddress}
).Where(c => c.FirstName.StartsWith("S"));
SelectMany
The SelectMany operation provides the capability to combine multiple from clauses, merging the results
of each object into a single sequence. Here??™s an example:
string[] owners =
{ new name { FirstName = "Scott", "Chris",
Pets = new List
{"Yukon", "Fido"}},
new name { FirstName = "Jason", "Steve",
Pets = new List{"Killer", "Fluffy"}},
new name { FirstName = "John", "Joe",
Pets = new List{"Spike", "Tinkerbell"}}}
IEnumerable query =
names.
Pages:
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124