Also note the 'query' equivalent of the lambda code as commented out section .(Eventhough the query expression is easier to write, this examples are all in lambda expression of people who are interested in lambdas)
private void SelectManyCompound()
{
List
new Product { productID=1, productName="Prod1", productPrice=100.50, stockQuantity= 10 },
new Product { productID=2, productName="Prod2", productPrice=89.50, stockQuantity= 15 },
new Product { productID=3, productName="Prod3", productPrice=10.00, stockQuantity= 120 },
new Product { productID=4, productName="Prod4", productPrice=19.50, stockQuantity= 30 },
};
List
new Order { CustomerName="Ben", OrderNo=1, TotalAmt= 1000},
new Order { CustomerName="Alice", OrderNo=2, TotalAmt= 900},
new Order { CustomerName="Sam", OrderNo=3, TotalAmt= 700},
};
var v = prodA.Join(orderA, a => true, b => true,
(a, b) =>
new
{
Prod = a,
order = b
})
.Where( (c) => c.Prod.productPrice > 75 );
/* Equivalent code using Query
var v = from a in prodA
from b in orderA
where a.productPrice > 75
select new { Prod = a, order = b };
*/
foreach (var v1 in v)
{
Console.WriteLine("ProdId:{0}, ProdName:{1}, Price:{2}, Qty:{3}, CustNm:{4}, OrdNo:{5},Total:{6}",
v1.Prod.productID,v1.Prod.productName,v1.Prod.productPrice,v1.Prod.stockQuantity,
v1.order.CustomerName,v1.order.OrderNo,v1.order.TotalAmt
);
}
}
OUTPUT
ProdId:1, ProdName:Prod1, Price:100.5, Qty:10, CustNm:Ben, OrdNo:1,Total:1000
ProdId:1, ProdName:Prod1, Price:100.5, Qty:10, CustNm:Alice, OrdNo:2,Total:900
ProdId:1, ProdName:Prod1, Price:100.5, Qty:10, CustNm:Sam, OrdNo:3,Total:700
ProdId:2, ProdName:Prod2, Price:89.5, Qty:15, CustNm:Ben, OrdNo:1,Total:1000
ProdId:2, ProdName:Prod2, Price:89.5, Qty:15, CustNm:Alice, OrdNo:2,Total:900
ProdId:2, ProdName:Prod2, Price:89.5, Qty:15, CustNm:Sam, OrdNo:3,Total:700
No comments:
Post a Comment