What are two possible ways to achieve this goal?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server 2008 database.
You create classes by using LINQ to SQL based on the records shown in the exhibit:

You need to create a LINQ query to retrieve a list of objects that contains the OrderID and CustomerID properties.
You need to retrieve the total price amount of each Order record. What are two possible ways to achieve this goal?
(Each correct answer presents a complete solution. Choose two.)

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server 2008 database.
You create classes by using LINQ to SQL based on the records shown in the exhibit:

You need to create a LINQ query to retrieve a list of objects that contains the OrderID and CustomerID properties.
You need to retrieve the total price amount of each Order record. What are two possible ways to achieve this goal?
(Each correct answer presents a complete solution. Choose two.)

A.
from details in dataContext.Order_Detail
group details by details.OrderID
into g
join order in dataContext.Orders on g.Key equals order.OrderID
select new
{
OrderID = order.OrderID,
CustomerID = order.CustomerID,
TotalAmount = g.Sum(od => od.UnitPrice * od.Quantity)
}

B.
dataContext.Order_Detail.GroupJoin(dataContext.Orders, d => d.OrderID, o => o.OrderID,
(dts, ord) => new {
OrderID = dts.OrderID,
CustomerID = dts.Order.CustomerID,
TotalAmount = dts.UnitPrice * dts.Quantity
})

C.
from order in dataContext.Orders
group order by order.OrderID into g
join details in dataContext.Order_Detail on g.Key equals details.OrderID select new
{
OrderID = details.OrderID,
CustomerID = details.Order.CustomerID,
TotalAmount = details.UnitPrice * details.Quantity
}

D.
dataContext.Orders.GroupJoin(dataContext.Order_Detail, o => o.OrderID, d => d.OrderID,
(ord, dts) => new {
OrderID = ord.OrderID,
CustomerID = ord.CustomerID,
TotalAmount = dts.Sum(od => od.UnitPrice * od.Quantity)
})

Explanation:
Alterantive A. This is an Object Query. It looks at the Order Details EntitySet and creating a group g based on OrderID.
* The group g is then joined with Orders EntitySet based on g.Key = OrderID
* For each matching records a new dynamic object containing OrderID, CustomerID and TotalAmount is created.
* The dynamic records are the results returned in an INumerable Object, for later processing

Alterantive D. This is an Object Query. The GroupJoin method is used to join Orders to OrderDetails.
Parameters for GroupJoin:
1. An Order_Details EntitySet
2. Order o (from the Orders in the Orders Entity Set, picking up Order_id from both Entity Sets)
3. Order_ID from the first Order_Details record from the OD EnitySet
4. Lamda Expression passing ord and dts (ord=o, dts=d)
The body of the Lamda Expression is working out the total and Returning a Dynamic object as in A.



Leave a Reply 2

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


John Galt

John Galt

I verified answer A is the correct answer using LINQPad. Also the explanation says that A is an Object Query, but it’s not. D is an Object Query. A is a LINQ query.

Here’s the LINQPad command I used to verify answer A:

from details in OrderDetails
group details by details.OrderId into grouping
join order in Orders on grouping.Key equals order.OrderId
select new
{
OrderId = order.OrderId,
CustomerId = order.CustomerId,
TotalAmount = grouping.Sum(detail => detail.UnitPrice * detail.Quantity)
}

John Galt

John Galt

Verified answer D as well, using Visual Studio.