var query = from c in contact
where c.LastName.Equals("Kleinerman")
select c.FirstName;
When this query executes, you receive the error that the sequence contains more than one element
because there are two contacts whose last name equals ??????Kleinerman.??™??™
You can also specify criteria to this operator as a parameter, as shown in this example:
var query2 = from c in contact
select c.LastName;
listBox1.Items.Add(query2.Single(con => con.Equals("Kobylinski")));
81
Part I: Introduction to Project LINQ
SingleOrDefault
Similar to the Single operator, the SingleOrDefault operator returns a single element from a sequence,
but it also returns a default value if no element is found. Again, use this operator only if you know
that your query will return a single element or that the element will be null when returned. If you use
SingleOrDefault and the sequence returns multiple elements, an exception is thrown.
Here??™s a query to the Contact table looking for all contacts whose last name equals ??????Kobylinski??™??™ and,
if any are found, returning the contact??™s first name.
Pages:
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163