FirstName.StartsWith("S") @@ta
&& a.LastName.StartsWith("A"));
Let??™s complicate things a bit more and add the Orderby clause:
IEnumerable
query =
from c in contact
where c.FirstName.StartsWith("S")
&& c.LastName.StartsWith("A")
43
Part I: Introduction to Project LINQ
Orderby c.LastName
select c;
This query expression would be written as method syntax as follows:
IEnumerable query = contact.Where(c => c.FirstName.StartsWith("S")
&& c.LastName.StartsWith("A")).OrderBy(c => c.LastName);
Run both versions of these queries (method syntax and query syntax), and you??™ll see that the output is
identical. What makes the method syntax possible is lambda expressions, which were discussed
in Chapter 2, ??????A Look at Visual Studio 2008.??™??™
Although query syntax is recommended over method syntax, there are times when method syntax is
preferred, such as in those queries that return the number of elements that match a specified condition.
Which Do You Use?
Given all of the information discussed in this chapter, the question might arise, ??????Which do I use, query
syntax or method syntax???™??™ The general rule is to use whichever syntax will make your code most readable,
which most often means using query syntax.
Pages:
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109