使用diff结构C#合并两个列表
发布时间:2020-12-15 08:23:20 所属栏目:百科 来源:网络整理
导读:我有一个结构对象列表 string source,string target,int count 样本数据: sourcea targeta 10sourcea targetb 15sourcea targetc 20 我的另一个对象列表是结构 string source,int addnvalueacount,int addnvaluebcount,int addnvalueccount 样本数据: sour
我有一个结构对象列表
string source,string target,int count 样本数据: sourcea targeta 10 sourcea targetb 15 sourcea targetc 20 我的另一个对象列表是结构 string source,int addnvalueacount,int addnvaluebcount,int addnvalueccount 样本数据: sourcea 10 25 35 我希望将第二个列表更改为第一个列表结构,然后使用第一个列表进行联合all(concat). 所以结果应该如下所示: sourcea targeta 10 sourcea targetb 15 sourcea targetc 20 sourcea addnlvaluea 10 sourcea addnlvalueb 25 sourcea addnlvaluec 35 所有帮助都是真诚的感谢.. 谢谢 解决方法
我推荐Concat和SelectMany;为你提供
List<A> listA = new List<A> { new A ("sorcea","targeta",10),new A ("sorcea","targetb",15),"targetc",20),}; List<B> listB = new List<B> { new B ("sourcea",10,15,35),}; 为了Concat你要做的就是添加SelectMany: var result = listA .Concat(listB .SelectMany(item => new [] { // turn single B item into three A new A(item.source,"addnvaluea",item.addnvalueacount),new A(item.source,"addnvalueb",item.addnvaluebcount),"addnvaluec",item.addnvalueccount),})); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |