Name);
foreach (var itms in icecreams)
Console.WriteLine(itms.Name);
This example displays item name values from the rows returned by the query.
The query returned the result-set into the variable icecreams, which is of type
Items. The three foreach loops use the same variable to get the items information
display. Here the query gets executed three times, one each at the foreach statement
execution. This process is time consuming, and also the execution gives poor
performance. This execution is called deferred execution.
Chapter 4
[ 99 ]
There is a way to eliminate this process of multiple executions for the same query.
Just convert the results into an array or a list using the operators ToList and
ToArray. So the previous code will look like this:
var icecreams = from cat in db.Items
where cat.CategoryID == 1
select new { cat.Name, cat.Categories.Description };
var lst = icecreams.ToList();
foreach (var itms in lst)
Console.WriteLine(itms.Name);
foreach (var itms in lst)
Console.WriteLine(itms.
Pages:
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171