As you learned in Chapter 3, you can force immediate execution of a query that does not produce a
singleton value by using the ToList() or ToArray() methods.
Remember query execution this way: deferred execution should be used to produce a sequence of values.
Immediate execution is used to return a singleton value such a Count, or Average.
253
Part III: LINQ to SQL
The following example counts the number of order items for a specific order where the unit price of the
item is less than $200. Because the query is returning a singleton value, it is executed immediately.
Table
orderdetail = context.GetTable();
var query =
from od in orderdetail
where od.SalesOrderID = 43662
select od;
listBox1.Items.Add(query.Count(orderdetail => orderdetail.UnitPrice < 200));
DataShape Class
LINQ to SQL provides a way to return related objects at the same time as your parent object with the
added benefit of returning only what you need. This is made possible by the DataShape class, which lets
you define a subtype that can be returned at the same time as the main query.
Pages:
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409