You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database. The application uses the ADO.NET Entity
Framework to model entities. The database includes objects based on the exhibit. (Click the Exhibit button.)
The application includes the following code segment. (Line numbers are included for reference only.)
01 using (AdventureWorksEntities advWorksContext = new AdventureWorksEntities()){
02 …
03 }
You need to retrieve a list of all Products from todays sales orders for a specified customer.
You also need to ensure that the application uses the minimum amount of memory when retrieving the list.
Which code segment should you insert at line 02?
A.
Contact customer = context.Contact.Where(“it.ContactID = @customerId”, new ObjectParameter(“customerId”, customerId)).First();
customer.SalesOrderHeader.Load();
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
order.SalesOrderDetail.Load();
if (order.OrderDate.Date == DateTime.Today.Date)
{
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format(“Product: {0} “, item.ProductID));
}
}
}
B.
Contact customer = context.Contact.Where(“it.ContactID = @customerId”, new ObjectParameter(“customerId”, customerId)).First();
customer.SalesOrderHeader.Load();
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
if (order.OrderDate.Date == DateTime.Today.Date)
{
order.SalesOrderDetail.Load();
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format(“Product: {0} “, item.ProductID));
}
}
}
C.
Contact customer = (from contact in context.Contact.Include(“SalesOrderHeader”) select contact).FirstOrDefault();
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
order.SalesOrderDetail.Load();
if (order.OrderDate.Date == DateTime.Today.Date)
{
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format(“Product: {0} “, item.ProductID));
}
}
}
D.
Contact customer = (from contact in context.Contact.Include(“SalesOrderHeader.SalesOrderDetail”) select contact).FirstOrDefault();
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
if (order.OrderDate.Date == DateTime.Today.Date)
{
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format(“Product: {0} “, item.ProductID));
}
}
}
Explanation:
A & C check the Order date after Order Detail, so we are retrieving more Order details than necessary
D is calling a Function (using eager loading) for the First Contact record only, so does not meet the requirements.