Evaluates all collection elements under the collection of a collection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.