博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
求一个集合的集合下所有集合元素求值
阅读量:7071 次
发布时间:2019-06-28

本文共 1941 字,大约阅读时间需要 6 分钟。

 

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

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

public class Order    {        public int OrderId { get; set; }        public ICollection
Suppliers { get; set; } public Order() { Suppliers = new List
(); } } public class Supplier { public int SupplierId { get; set; } public ICollection
Products { get; set; } public Supplier() { Products = new List
(); } } 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
{ new Order { OrderId = 1, Suppliers = new List
{ new Supplier {SupplierId=11, Products= new List
{ new Product {ProductId=111, Quantity=1, UnitPrice=10 }, new Product {ProductId = 112, Quantity =1, UnitPrice = 20 } } }, new Supplier {SupplierId =12, Products = new List
{ 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(); }

 

转载地址:http://xmhll.baihongyu.com/

你可能感兴趣的文章
azkaben任务调度器
查看>>
L102
查看>>
New Concept English Two 7
查看>>
极致精简的webservice例子
查看>>
Django的cookie学习
查看>>
HTML5之美二 --- 转载
查看>>
python全栈开发 * 11知识点汇总 * 1806011
查看>>
@override报错
查看>>
POJ3068:"Shortest" pair of paths——题解
查看>>
js判断当前时间前几天和格式校验
查看>>
Linux (Ubuntu)安装ssh
查看>>
详细解释:nginx中ChsHttpIndexModule模块配置及各个参数含义
查看>>
20165306 第四周学习任务
查看>>
手把手教你用动软.NET代码生成器实例教程
查看>>
栈分配的速度快于堆
查看>>
[转] 使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
查看>>
CF-Pasha and Tea(贪心6)
查看>>
ASCII_01
查看>>
Mac控制远程Linux服务器
查看>>
R语言基础命令与安装
查看>>