The element returned
from the selector method could be an object or a collection. If it is a collection, the
programmer has to take care of reading the collection and returning the values.
The following example creates a sequence of the names of all items:
IEnumerable
icecreamNames = items.Select(itm => itm.Name);
Following is an equivalent query of the above expression:
IEnumerable icecreamsNames = from itm in items
select itm.Name;
The following code returns items with a price less than 10:
IEnumerable- lowPricedItems =
from item in items
where item.Price < 10
select item;
Chapter 7
[ 177 ]
Console.WriteLine("Items with low price:");
foreach (var item in lowPricedItems)
{
Console.WriteLine("Price of {0} is {1} ", item.Name, item.Price);
}
The following expression is another example to retrieve items and their prices where
the price is less than 10:
var IcecreamsPrices = items.Where(itm => itm.Price < 10)
.Select(itm => new { itm.Name, itm.Price })
.ToList();
foreach (var ices in IcecreamsPrices)
{
Console.
Pages:
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269