DataContext context = new DataContext("Initial Catalog=AdventureWorks;@@ta
Integrated Security=sspi");
Table
contact = context.GetTable();
IEnumerable query =
contact.AsEnumerable().Where(con => con.FirstName.Contains("K"));
foreach (Contact item in query)
listBox1.Items.Add(item.FirstName);
The results of this query contain all the contact first names that contain the letter K. Here are
partial results:
Kim
Keyley
Karel
Karen
Kris
Kevin
...
In this example, the System.Query.Sequence implementation of Where is utilized, but in the next
example, the Where() method with a predicate is used:
IEnumerable query = contact.Where(=> con.FirstName.Contains("K"));
Cast
The Cast operator casts the element of an IEnumerable collection to a specified type. The benefit of this
is that by supplying necessary type information, you can invoke standard query operators on nongeneric
collections.
The following example uses an ArrayList as a data source. An ArrayList does not implement
IEnumerable(Of T), but by using the Cast operator you can use the standard query operators, such
as Select, to query the sequence.
Pages:
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150