Evaluate all set elements in a set

Source: Internet
Author: User

Evaluate all set elements in a set

 

The scenario is as follows: Order has a Suppler set, that is, there may be multiple suppliers under an Order; Supplier has a set of products, that is, purchasing multiple products for a Supplier.

The requirement is as follows: calculate the total price of all purchased products

The model is as follows:

    public class Order    {        public int OrderId { get; set; }        public ICollection<Supplier> Suppliers { get; set; }        public Order()        {            Suppliers = new List<Supplier>();        }    }    public class Supplier    {        public int SupplierId { get; set; }        public ICollection<Product> Products { get; set; }        public Supplier()        {            Products = new List<Product>();        }    }    public class Product    {        public int ProductId { get; set; }        public decimal UnitPrice { get; set; }        public int Quantity { get; set; }    }

 

Calculate the total price as follows:

        static void Main(string[] args)        {            var orders = new List<Order>            {                new Order                {                    OrderId = 1,                    Suppliers = new List<Supplier>                    {                        new Supplier {SupplierId=11, Products= new List<Product>                        {                            new Product {ProductId=111, Quantity=1, UnitPrice=10 },                            new Product {ProductId = 112, Quantity =1, UnitPrice = 20 }                        } },                        new Supplier {SupplierId =12, Products = new List<Product>                        {                            new Product {ProductId =113, Quantity =2, UnitPrice=30 },                            new Product {ProductId=114, Quantity=1, UnitPrice=50 }                        } }                    }                }            };            var suppliers = orders.Select(t => t.Suppliers);            var productTotalPrice = suppliers.Sum(t => t.Sum(p => p.Products.Sum(o => o.Quantity * o.UnitPrice)));            Console.WriteLine(productTotalPrice);            Console.ReadKey();        }

 

Related Article

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.