C# 如何在List里根据某一字段作为条件,将剩下的值对应相加

肆年 2017-05-07 05:24:05
如定义list :
list.Add(new model { name = "Cash", Pay = 500, Dicount = 2 });
list.Add(new model { name = "Bank", Pay = 1200, Dicount = 0 });
list.Add(new model { name = "Cash", Pay = 600, Dicount = 2 });

实现成这样的:
{ name = "Cash", Pay = 1100, Dicount = 4 };
{ name = "Bank", Pay = 1200, Dicount = 0 };

是否需要Copy一下原list再进行?
...全文
1914 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
肆年 2017-05-08
  • 打赏
  • 举报
回复
引用 10 楼 ducker3590 的回复:
引用 8 楼 u013718767 的回复:
[quote=引用 4 楼 ducker3590 的回复:]
您好,我想再问一下,如果要只指定“Cash”求和,多条Bank不求和,该怎么写?谢谢。
分开处理 处理完之后组合 [/quote]非常非常的感谢。 昨晚睡前想起一种方法,实现了,又拿您的方法修改试了一下。 分享出来一下: 第一种
            //******第一种*****//
            List<OrderPay> OrderPays = new List<OrderPay>(); //这个里面是有支付方式及支付值的
            List<OrderPay> orderPaysMerge = new List<OrderPay>(); //新定义的
            orderPaysMerge.Clear();
            for (int i = OrderPays.Count - 1; i >= 0; i--) //倒序剔除
            {
                if (OrderPays[i].PayName == "Cash")
                {
                    orderPaysMerge.Add(OrderPays[i]);
                    OrderPays.Remove(OrderPays[i]);
                }
            }
            var mergeResult = (from orderpay in orderPaysMerge
                         group orderpay by orderpay.PayName into g
                         select new OrderPay
                         {
                             PayName = g.Key,
                             PayValue = g.Sum(m => m.PayValue)
                         }).ToList();

            foreach (var item in mergeResult)
            {
                OrderPays.AddRange(mergeResult);
            }

            //最后拿List OrderPays做最后运算
第二种:
      //*****第二种*****//
            List<OrderPay> OrderPays = new List<OrderPay>(); //这个里面是有支付方式及支付值的
            List<OrderPay> orderPaysMerge = new List<OrderPay>();
            var mergeResult = (from orderpay in OrderPays
                               group orderpay by orderpay.PayName into g
                               where g.Key == "Cash"
                               select new OrderPay
                               {
                                   PayName= g.Key,
                                   PayValue = g.Sum(m => m.PayValue)
                               }).ToList();  //转换成List方便后面使用

            var removeResult = OrderPays.Where(m => m.PayName != "Cash").ToList();

            removeResult.AddRange(mergeResult);

            //最后拿List removeResult做下一步运算
csdnFUCKINGSUCKS 2017-05-07
  • 打赏
  • 举报
回复
引用 8 楼 u013718767 的回复:
引用 4 楼 ducker3590 的回复:
您好,我想再问一下,如果要只指定“Cash”求和,多条Bank不求和,该怎么写?谢谢。

分开处理 处理完之后组合
肆年 2017-05-07
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
            list.Add(new model { name = "Cash", Pay = 500, Dicount = 2 });
            list.Add(new model { name = "Bank", Pay = 1200, Dicount = 0 });
            list.Add(new model { name = "Cash", Pay = 600, Dicount = 2 });

            var g = list.GroupBy(x => x.name);
            var a = g.Select(x => new { name = x.Key, Pay = x.Sum(y => y.Pay), Dicount = x.Sum(y => y.Dicount) });
            foreach(var x in a) Console.WriteLine(x);
您好,我想再问一下,如果要只指定“Cash”求和,多条Bank不求和,该怎么写?谢谢
肆年 2017-05-07
  • 打赏
  • 举报
回复
引用 4 楼 ducker3590 的回复:
您好,我想再问一下,如果要只指定“Cash”求和,多条Bank不求和,该怎么写?谢谢。
xuzuning 2017-05-07
  • 打赏
  • 举报
回复
            list.Add(new model { name = "Cash", Pay = 500, Dicount = 2 });
list.Add(new model { name = "Bank", Pay = 1200, Dicount = 0 });
list.Add(new model { name = "Cash", Pay = 600, Dicount = 2 });

var g = list.GroupBy(x => x.name);
var a = g.Select(x => new { name = x.Key, Pay = x.Sum(y => y.Pay), Dicount = x.Sum(y => y.Dicount) });
foreach(var x in a) Console.WriteLine(x);
肆年 2017-05-07
  • 打赏
  • 举报
回复
引用 3 楼 wxmtgg 的回复:
你是在写一个收银软件吗?我这有一个现成的,加我QQ:81471644 发给你参考一下
已经存在,只是在增加需求。
肆年 2017-05-07
  • 打赏
  • 举报
回复
引用 2 楼 wxmtgg 的回复:
public static void show(ListView listview,Label label1) { if (tempB == true) { listview.Items.Clear(); ZongQianShu = 0; for (int i = 0; i < xiaoshouList.Count; i++) { System.Windows.Forms.ListViewItem lit = listview.Items.Add((i + 1).ToString()); lit.SubItems.Add(xiaoshouList[i].tm); lit.SubItems.Add(xiaoshouList[i].Name); lit.SubItems.Add("1"); lit.SubItems.Add(xiaoshouList[i].Shoujia); ZongQianShu = ZongQianShu + float.Parse(xiaoshouList[i].Shoujia); } label1.Text = "总计:" + zlc.ZongQianShu + " 元"; if (Byykg == true && tempB == true && zlc.xiaoshouList.Count > 0) { text = xiaoshouList[xiaoshouList.Count - 1].Shoujia;//朗读内容 ThreadStart ts = new ThreadStart(langdu); //初始化Thread的新实例,并通过构造方法将委托ts做为参数赋初始值。 Thread td = new Thread(ts); //需要引入System.Threading命名空间 td.Start(); //运行委托 } } else { if (Byykg) { play(WavPath + "\\无此条码.wav"); } alter a = new alter(true,XinPinTiaoMa); a.ShowDialog(); } } 这个是完整的一个方法
谢谢,但我觉得,您可能错误理解我的意思了。
csdnFUCKINGSUCKS 2017-05-07
  • 打赏
  • 举报
回复
wxmtgg 2017-05-07
  • 打赏
  • 举报
回复
你是在写一个收银软件吗?我这有一个现成的,加我QQ:81471644 发给你参考一下
wxmtgg 2017-05-07
  • 打赏
  • 举报
回复
public static void show(ListView listview,Label label1) { if (tempB == true) { listview.Items.Clear(); ZongQianShu = 0; for (int i = 0; i < xiaoshouList.Count; i++) { System.Windows.Forms.ListViewItem lit = listview.Items.Add((i + 1).ToString()); lit.SubItems.Add(xiaoshouList[i].tm); lit.SubItems.Add(xiaoshouList[i].Name); lit.SubItems.Add("1"); lit.SubItems.Add(xiaoshouList[i].Shoujia); ZongQianShu = ZongQianShu + float.Parse(xiaoshouList[i].Shoujia); } label1.Text = "总计:" + zlc.ZongQianShu + " 元"; if (Byykg == true && tempB == true && zlc.xiaoshouList.Count > 0) { text = xiaoshouList[xiaoshouList.Count - 1].Shoujia;//朗读内容 ThreadStart ts = new ThreadStart(langdu); //初始化Thread的新实例,并通过构造方法将委托ts做为参数赋初始值。 Thread td = new Thread(ts); //需要引入System.Threading命名空间 td.Start(); //运行委托 } } else { if (Byykg) { play(WavPath + "\\无此条码.wav"); } alter a = new alter(true,XinPinTiaoMa); a.ShowDialog(); } } 这个是完整的一个方法
wxmtgg 2017-05-07
  • 打赏
  • 举报
回复
listview.Items.Clear(); ZongQianShu = 0; for (int i = 0; i < xiaoshouList.Count; i++) { System.Windows.Forms.ListViewItem lit = listview.Items.Add((i + 1).ToString()); lit.SubItems.Add(xiaoshouList[i].tm); lit.SubItems.Add(xiaoshouList[i].Name); lit.SubItems.Add("1"); lit.SubItems.Add(xiaoshouList[i].Shoujia); ZongQianShu = ZongQianShu + float.Parse(xiaoshouList[i].Shoujia); } label1.Text = "总计:" + zlc.ZongQianShu + " 元";

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧