There is MS Visual Studio 2010 and MS .NET Framework 4 application PassGuideApp.
PassGuideApp connects to a MS SQL Server 2008 database PassGuideDB.
LINQ to SQL is used to define classes.
There are two objects named Product and Products_details.
There is a one-to-many relation from Product to Product_details on the ProductID field.
The Products_details table include the fields Weight and NumberOf
The total weight of each Product need to be calculated.
How can this be achieved?
A.
dataContext.Product_Details GroupJoin (dataContext.Product, d => d.ProductID,
o => o.ProductlD, (dts, ord) => new { ProductID = dts.ProductID),
TotalAmount = dts.Weight * dts.NumberOf})
B.
from Product in dataContext.Product group Product by Product.ProductID into k join details in
dataContext.Product_Details on k.Key equals details.ProductID select new {ProductID =
details.ProductID, TotalAmount = details.Weight*details.NumberOf}
C.
from details in datacontext.Product_Details group details by details.ProductID into k join
Product in dataContext.Products on k.Key equals Product.ProductID select new {ProductID =
Product.ProductID), Total amount k.Sum(pd =>pd.Weight * pd.NumberOf)}
Explanation:
It’s the exact same question as question #20 before, except some of the answers are modified. As usual, I would vote for C, simply because it’s the only one containing ‘Sum’ keyword. But to explain the answer we have:
– from details in datacontext.Product_details group details by details.ProductID into k
so product_detail is being grouped by ProductID into a group ‘k’
Then we have:
– join Product in dataContext.Products on k.Key equals Product.ProductID
The statement above adds Product fields to our group, matching on ProductID, not really sure why they bother doing it, because they only use the ProductID field, which already exists in Product_Details, but whatever. We end up with the following statement which ‘shapes’ the output:
– select new
{
ProductID = Product.ID,
TotalAmount = k.Sum(pd => pd.Weight * pd.NumberOf)
}
Which creates an output with ProductID and TotalAmount columns.