zoukankan      html  css  js  c++  java
  • 求一个集合的集合下所有集合元素求值

    场景是这样的:Order下有一个Suppler的集合,即一个订单下可能有多个供应商;Supplier下有一个Product的集合,即对一个供应商采购多个产品。

    需求是这样的:算出所有订购产品的总价

    模型这样:

        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; }
        }

    这样算出总价:

            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();
            }
  • 相关阅读:
    解压cpio.gz文件
    get/post时中文乱码问题的解决办法(转载)
    linux下卸载oracle 10g
    linux下oracle自启动
    linux mount远程磁盘(转载)
    转载JS编写随机全屏浮动窗口
    linux下配置vsftpd(FTP)
    rhel 6安装oracle 11g R2
    MSSQL数据库备份还原常用SQL语句及注意
    总结一下,写的很差!还是抄吧,不丢人了。
  • 原文地址:https://www.cnblogs.com/darrenji/p/5216278.html
Copyright © 2011-2022 走看看