Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

392

Part III Creating Components

Id: 1, Name: Janet Gates, Dept: IT

Id: 2, Name: Orlando Gee, Dept: Marketing

Id: 3, Name: Eric Lang, Dept: Sales

Id: 4, Name: Keith Harris, Dept: IT

Id: 5, Name: David Liu, Dept: Marketing

Id: 6, Name: Lucy Harrington, Dept: Sales

Notice that this time, the second time the application iterates through the allEmployees

collection, the list displayed does not include Donald Blanton. This is because the query is evaluated and the results cached before Donald Blanton is added to the empTree bi-

nary tree.

9.Press Enter to return to Visual Studio 2008.

If you want to continue to the next chapter:

Keep Visual Studio 2008 running, and turn to Chapter 21. If you want to exit Visual Studio 2008 now:

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project.

Chapter 20 Quick Reference

To

Do this

Project specified fields from an enumerable collection

Use the Select method, and specify a lambda expression that identifies the fields to project. For example:

var customerFirstNames = customers.Select(cust => cust. FirstName);

Or use the from and select query operators. For example:

var customerFirstNames = from cust in customers select cust.FirstName;

Filter rows from an enumerable collection

Use the Where method, and specify a lambda expression containing the criteria that rows should match. For example:

var usCompanies = addresses.Where(addr =>

String.Equals(addr.Country, “United States”)). Select(usComp => usComp.CompanyName);

Or use the where query operator. For example:

var usCompanies =

from a in addresses

where String.Equals(a.Country, “United States”) select a.CompanyName;

Chapter 20 Querying In-Memory Data by Using Query Expressions

393

Enumerate data in a specific order

Use the OrderBy method, and specify a lambda expression identifying the field to use to order rows. For example:

var companyNames =

addresses.OrderBy(addr => addr.CompanyName). Select(comp => comp.CompanyName);

Or use the orderby query operator. For example:

var companyNames = from a in addresses

orderby a.CompanyName select a.CompanyName;

Group data by the values in a field

Use the GroupBy method, and specify a lambda expression identifying the field to use to group rows. For example:

var companiesGroupedByCountry = addresses.GroupBy(addrs => addrs.Country);

Or use the group by query operator. For example:

var companiesGroupedByCountry = from a in addresses

group a by a.Country;

Join data held in two different collections

Use the Join method specifying the collection to join with, the join riteria, and the fields for the result. For example:

var citiesAndCustomers = customers.

Select(c => new { c.FirstName, c.LastName, c.CompanyName }). Join(addresses, custs => custs.CompanyName,

addrs => addrs.CompanyName,

(custs, addrs) => new {custs.FirstName, custs.LastName,

 

addrs.Country });

 

Or use the join query operator. For example:

 

var citiesAndCustomers =

 

from a in addresses

 

join c in customers

 

on a.CompanyName equals c.CompanyName

 

select new { c.FirstName, c.LastName, a.Country };

Force immediate generation

Use the ToList or ToArray method to generate a list or an array containing

of the results for a LINQ query

the results. For example:

 

var allEmployees =

 

from e in empTree.ToList<Employee>()

 

select e;

 

 

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]