The scenario is this: there is a collection of suppler under order, that is, there may be multiple vendors under an order; supplier has a collection of product, which is the purchase of multiple products from one vendor.
The demand is this: figure out the total price of all ordered products
Model This way:
Public classOrder { Public intOrderId {Get;Set; } PublicIcollection<supplier> Suppliers {Get;Set; } PublicOrder () {Suppliers=NewList<supplier>(); } } Public classSupplier { Public intSupplierId {Get;Set; } PublicIcollection<product> Products {Get;Set; } PublicSupplier () { Products=NewList<product>(); } } Public classProduct { Public intProductId {Get;Set; } Public decimalUnitPrice {Get;Set; } Public intQuantity {Get;Set; } }
This calculates the total price:
Static voidMain (string[] args) { varOrders =NewList<order> { NewOrder {OrderId=1, Suppliers=NewList<supplier> { NewSupplier {supplierid= One, products=NewList<product> { NewProduct {productid=111, quantity=1, unitprice=Ten }, NewProduct {ProductId = the, Quantity =1, UnitPrice = - } } }, NewSupplier {SupplierId = A, products =NewList<product> { NewProduct {ProductId =113, Quantity =2, unitprice= - }, NewProduct {productid= the, quantity=1, unitprice= - } } } } } }; varsuppliers = orders. Select (t =t.suppliers); varProducttotalprice = suppliers. Sum (t = t.sum (p = p.products.sum (o = o.quantity *( O.unitprice))); Console.WriteLine (Producttotalprice); Console.readkey (); }
Evaluates all collection elements under the collection of a collection