Which code segment should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application connects to a Microsoft SQL Server database. You use the ADO.NET LINQ to SQL model
to retrieve data from the database. The application contains the Category and Product entities, as
shown in the following exhibit.

You need to ensure that LINQ to SQL executes only a single SQL statement against the database. You
also need to ensure that the query retrieves the list of categories and the list of products. Which
code segment should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application connects to a Microsoft SQL Server database. You use the ADO.NET LINQ to SQL model
to retrieve data from the database. The application contains the Category and Product entities, as
shown in the following exhibit.

You need to ensure that LINQ to SQL executes only a single SQL statement against the database. You
also need to ensure that the query retrieves the list of categories and the list of products. Which
code segment should you use?

A.
using (NorthwindDataContext dc = new NorthwindDataContext())
{
dc.ObjectTrackingEnabled = false;
var categories = from c in dc.Categories
select c;
foreach (var category in categories)
{
Console.WriteLine(“{0} has {1} products”, category.CategoryName, category.Products.Count);
}

}

B.
using (NorthwindDataContext dc = new NorthwindDataContext())
{
dc.DeferredLoadingEnabled = false;
DataLoadOptions dlOptions = new DataLoadOptions();
dlOptions.LoadWith<Category>(c => c.Products);
dc.LoadOptions = dlOptions;
var categories = from c in dc.Categories
select c;
foreach (var category in categories)
{
Console.WriteLine(“{0} has {1} products”, category.CategoryName, category.Products.Count);
}
}

C.
using (NorthwindDataContext dc = new NorthwindDataContext())
{
dc.DeferredLoadingEnabled = false;
var categories = from c in dc.Categories
select c;
foreach (var category in categories)
{
Console.WriteLine(“{0} has {1} products”, category.CategoryName, category.Products.Count);
}
}

D.
using (NorthwindDataContext dc = new NorthwindDataContext())
{
dc.DeferredLoadingEnabled = false;
DataLoadOptions dlOptions = new DataLoadOptions();
dlOptions.AssociateWith<Category>(c => c.Products);
dc.LoadOptions = dlOptions;
var categories = from c in dc.Categories
select c;
foreach (var category in categories)
{
Console.WriteLine(“{0} has {1} products”, category.CategoryName, category.Products.Count);
}
}

Explanation:
DataLoadOptions Class Provides for immediate loading and filtering of related data.
DataLoadOptions.LoadWith(LambdaExpression) Retrieves specified data related to the main target
by using a lambda expression.
You can retrieve many objects in one query by using LoadWith.
DataLoadOptions.AssociateWith(LambdaExpression) Filters the objects retrieved for a particular
relationship.

Use the AssociateWith method to specify sub-queries to limit the amount of retrieved data.
DataLoadOptions Class
(http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx)
How to: Retrieve Many Objects At Once (LINQ to SQL)
(http://msdn.microsoft.com/en-us/library/Bb386917(v=vs.90).aspx)
How to: Filter Related Data (LINQ to SQL)
(http://msdn.microsoft.com/en-us/library/Bb882678(v=vs.100).aspx)



Leave a Reply 0

Your email address will not be published. Required fields are marked *