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 147 | Next

Scott Klein

"Professional LINQ"

FirstName.StartsWith("S")
select c.FirstName;
listBox1.Items.Add(query.First());
This method throws an exception if the source sequence contains no elements. Use the FirstOrDefault
operator if there is a possibility that the source might be empty.
You can also add specific criteria when using this operator. The following code returns the first element
that satisfies a specific condition, the first name whose length is greater than 5.
listBox1.Items.Add(query.First(name => name.Length > 5));
79
Part I: Introduction to Project LINQ
Last
The opposite of the First operator, the Last operator returns the last element in a collection. Here the
Contact table is queried looking for all contacts whose first name begins with the letter S, and the Last
operator returns the last element from the returned collection:
DataContext context = new DataContext("Initial Catalog = @@ta
AdventureWorks;Integrated Security=sspi");
Table contact = context.GetTable();
var query = from c in contact
where c.FirstName.StartsWith("S")
select c.


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