请教 List 类型的合并写法?

marklr 2016-07-05 11:24:36

//我有以下自定义类型
public class theTypeNameMsg
{
public string theTypeName { set; get; } //姓名
public double KuCun { set; get; } //库存数量
public theTypeNameMsg(string T_theTypeName, double T_KuCun)
{
theTypeName = T_theTypeName;
KuCun = T_KuCun;
}
}

List<theTypeNameMsg> t1 = new List<theTypeNameMsg>() { new theTypeNameMsg("小明", 95), new theTypeNameMsg("张三", 91) };
List<theTypeNameMsg> t2 = new List<theTypeNameMsg>() { new theTypeNameMsg("小明", 80), new theTypeNameMsg("张三", 82), new theTypeNameMsg("李四", 77) };


List<theTypeNameMsg> t3 = ........//合并t1和t2,相同姓名合并一起,且库存相加,t3最终结果只有3个元素,即:("小明", 175) 、("张三", 173)和("李四", 77),请教一个简洁的写法

...全文
323 13 打赏 收藏 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
token不能为空 2016-07-11
  • 打赏
  • 举报
回复
from in group by select 这种代码即美观又好理解,已经非常不错了,就别再foreach了
  • 打赏
  • 举报
回复
引用 8 楼 drifter2002 的回复:
用foreach 吧,简单。 linq搞复杂了
group by 其实是一个很标准的最基础的语句,如果觉得复杂,那就肯定没怎么学 linq。
  • 打赏
  • 举报
回复
引用 1 楼 qqamoon 的回复:
var t3 = t1.Union(t2) .GroupBy(g=>g.theTypeName) .Select(s=> new theTypeNameMsg (s.Key.theTypeName, s.Sum(m=>m.KuCun)) .ToList();
是这样的。 写成 sql 形式,更清晰一些。特别是比这个查询还复杂时。
  • 打赏
  • 举报
回复
var query = from x in t1.Concat(t2)
            group x by x.theTypeName into g
            select new theTypeNameMsg(g.Key, g.Sum(a => a.KuCun));
var t3 = query.ToList();
tcmakebest 2016-07-09
  • 打赏
  • 举报
回复

using System.Linq;
List<theTypeNameMsg> t3 = t1.Union(t2)
        .GroupBy(g => g.theTypeName)
        .Select(s => new theTypeNameMsg(s.Key, s.Sum(m => m.KuCun)))
        .ToList();
drifter2002 2016-07-08
  • 打赏
  • 举报
回复
用foreach 吧,简单。 linq搞复杂了
龍过鸡年 2016-07-05
  • 打赏
  • 举报
回复
引用 2 楼 marklr 的回复:
[quote=引用 1 楼 qqamoon 的回复:] var t3 = t1.Union(t2) .GroupBy(g=>g.theTypeName) .Select(s=> new theTypeNameMsg (s.Key.theTypeName, s.Sum(m=>m.KuCun)) .ToList();
运行不了啊 [/quote] 噢噢噢噢~~~我手打的,在 s.Sum(m=>m.KuCun)) 后面少了个括号
angle860123 2016-07-05
  • 打赏
  • 举报
回复
var t3 = t1.Union(t2).GroupBy(p => p.theTypeName).Select(p => new theTypeNameMsg(p.Key, p.Sum(item => item.KuCun))).ToList();
正怒月神 2016-07-05
  • 打赏
  • 举报
回复
public static void Main(string[] args)
        {
            //测试数据
            List<Student> s1=new List<Student>(){
            new Student(){name="1",score=10},
            new Student(){name="2",score=20},
            new Student(){name="3",score=30},
            };

            List<Student> s2 = new List<Student>(){
            new Student(){name="1",score=10},
            new Student(){name="2",score=20},
            new Student(){name="3",score=30},
            };

            //合并
            var q = s1.Concat(s2).GroupBy(x=>x.name).Select(x=>new Student{name=x.Key, score=x.Sum(y=>y.score)});
            //打印显示
            foreach (var item in q)
            {
                Console.WriteLine(item.name+"----"+item.score);
            }

            Console.ReadLine();
        }

        //测试类
        public class Student
        {
            public string name { get; set; }
            public int score { get; set; }
        }
leeya66 2016-07-05
  • 打赏
  • 举报
回复
最笨的办法,用foreach遍历,
marklr 2016-07-05
  • 打赏
  • 举报
回复
引用 1 楼 qqamoon 的回复:
var t3 = t1.Union(t2) .GroupBy(g=>g.theTypeName) .Select(s=> new theTypeNameMsg (s.Key.theTypeName, s.Sum(m=>m.KuCun)) .ToList();
运行不了啊
龍过鸡年 2016-07-05
  • 打赏
  • 举报
回复
var t3 = t1.Union(t2) .GroupBy(g=>g.theTypeName) .Select(s=> new theTypeNameMsg (s.Key.theTypeName, s.Sum(m=>m.KuCun)) .ToList();
巴士上的邂逅 2016-07-05
  • 打赏
  • 举报
回复
var t3 = t1.Concat(t2).GroupBy(a => a.theTypeName).Select(g => new theTypeNameMsg(g.Key, g.Sum(b => b.KuCun))).ToList();

111,128

社区成员

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

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

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