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.
dataContext.Products GroupJoin(datacontext.Product Details p => p.ProductlD, d => d,
ProductID, (prod, dts) => new (ProductID) = prod.ProductID, CustomerID = prod.CustomerlD,
TotalAmount = dts.Sum(pd => pd.Weight * pd.NumberOf))
C.
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}
Explanation:
http://www.dotnetperls.com/groupjoin
I don’t even see what the F is going on in these functions. Missing commas, wrong variable spelling. The only reason B makes sense, is because it’s the only answer that uses ‘Sum’ function.
Here’s a cleaned up version of the correct answer:
dataContext.Products.GroupJoin
(
datacontext.Product_Details,
p => p.ProdcutID,
d => d.ProeductID,
(prod, dts) => new
(ProductID) = prod.ProductID,
CustomerID = prod.CustomerID,
TotalAmount = dts.Sum(Pd => pd.Weight * pd.NumberOF)
)
Alright, let me try to explain this clusterfuck of an answer. GroupJoin is being called on Product collection:
dataContext.Products.GroupJoin
so that’s our primary collection.
Then according to the link (http://www.dotnetperls.com/groupjoin) we need to specify 4 arguments for the GroupJoin method.
Argument 1 is the secondary collection. In our case it’s Product_Details, so we have:
– dataContext.Product_Details,
Argument 2 is a Func that returns the key from the first object type. In our case we want ProductID, so we have:
– p => p.productID
Argument 3 is a Func that returns the key from the second object type. In our case it’s also ProductID, so we have:
– d => d.productID
Argument 4 is a Func that stores the grouped object with the group itself. We have:
– (prod, dts) => new {ProductID = prod.ProductID, CustomerID = prod.CustomerID, TotalAmount = dts.Sum(pd => pd.Weight * pd.NumberOf)}
Let me break down Argument 4 a bit more.
I’m guessing that the two input arguments to the lamda function are based on the collections we’re joining, so they are Products and Product_Details respectively (prod and dts).
Those two arguments are then used to ‘shape’ the output, so we get ProductID and CustomerID from the Products table, and combine it with TotalAmount which gets calculated from Product_Details table (using Weight and NumberOf fields).