Contacts
where con.LastName.StartsWith("K")
group con.FirstName by new { last = con.LastName, middle = con.MiddleName} ;
foreach (var grp in conQuery)
{
listBox1.Items.Add(grp.Key);
foreach (var listing in grp)
{
listBox1.Items.Add(listing);
}
}
Very slick.
Read-Only Data
If you don??™t plan to modify the data, you can get a pretty good performance increase by telling the
DataContext that you want the data returned as read-only, so this last section will discuss working with
read-only data.
To get read-only data, set the ObjectTrackingEnabled property to false. ObjectTrackingEnabled tells
the framework to track the original value. Setting it to false means that the framework doesn??™t need to
track changes, which provides a performance increase.
The following example shows how to disable object tracking by setting the ObjectTrackingEnabled
property to false.
255
Part III: LINQ to SQL
AdventureWorks db = new AdventureWorks("Integrated Security=sspi");
db.ObjectTracking = false;
var conQuery =
from con in db.Contacts
where con.
Pages:
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412