SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 150 | Next

Scott Klein

"Professional LINQ"


listBox1.Items.Add(query.LastOrDefault(name => name.Length < 5));
Single
The Single operator returns a single element from a sequence, or the only element that meets a given
condition. This operator should be used if you know that your query will return a single element. If the
sequence returns multiple elements and this operator is used, an exception is thrown.
The following example queries the Contact table for all contacts whose last name equals ??????Kobylinski,??™??™
and if any are found, returns the contact??™s first name. The Single operator returns the single element
from the returned collection.
DataContext context = new DataContext("Initial Catalog=AdventureWorks;Integrated
Security=sspi");
Table contact = context.GetTable();
var query = from c in contact
where c.LastName.Equals("Kobylinski")
select c.FirstName;
listBox1.Items.Add(query.Single());
When this query runs, the name Andrew is written to the list box, because that??™s the only contact with
the last name of Kobylinski. Now change the query to the following and rerun it.


Pages:
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6