OrderBy(a => a.LastName);
foreach (var item in query)
listBox1.Items.Add(item.FirstName + " " + item.LastName + " " +
item.EmailAddress);
Press F5 to compile and run the application. When the form opens, click the Method Syntax button. The
list box will be populated with the first names, last names, and email addresses of those contacts who
50
Chapter 3: LINQ Queries
meet the criteria specified in the query expression. The results on your form should look like the results
that were returned in the first example (shown in Figure 3-4).
This IEnumerable example can also be written to use automatic type deduction using the var keyword
as follows:
var query =
contact.Where(a => a.FirstName.StartsWith("S")
&& a.LastName.StartsWith("K")).OrderBy(a => a.LastName);
Each of these examples contains the three actions that make up a query operation. Those of you who
have read any of my previous books already know that I like to assign small ??????homework assignments,??™??™
which build on the examples of the current chapter.
Your homework assignment for this chapter is fairly simple.
Pages:
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117