当前位置: 首页 > 编程语言 > C#

如何合并两个不同对象的列表?分享

时间:2023-04-10 15:20:05 C#

如何合并两个不同对象的列表?使用C#和LINQ,如何合并两个不同对象的列表,比如研讨会和会议?它们有一些共同的和一些不同的字段/属性,并且不共享唯一的ID。classSeminar{intid,DateTimejoinDate,stringname}classConference{GuidconfNumber,DateTimejoinDateTypetype}我有一个列表:ListList我需要将它们合并成一个超级List:List代码片段会很有帮助。下面的代码对我来说很好用,如果这是你对MergeListsomeAs=newList(){newA(),newA()}的定义,这是一个解决方案;ListsomeBs=newList(){newB(),newB{something=newA()}};ListallS=(fromxinsomeAsselect(Object)x).ToList();allS.AddRange((fromxinsomeBsselect(Object)x).ToList());其中A和B是一些类,如下所示classA{publicstringsomeAnotherThing{get;放;}}classB{publicAsomething{get;放;}}另一个方案ListsomeAs=newList(){newA(),newA()};列出someBs=newList(){newB(),newB{something=string.Empty}};ListallS=(fromxinsomeAsselect(Object)new{someAnotherThing=x.someAnotherThing,something=string.Empty}).ToList();allS.AddRange((fromxinsomeBsselect(Object)new{someAnotherThing=string.Empty,something=x.something}).ToList());其中A和B的类定义为classA{publicstringsomeAnotherThing{get;放;}}classB{publicstringsomething{get;List,它相当简单:ListobjectList=seminarList.Cast().Concat(会议列表).ToList();如果那不是你想要的,那么你需要定义你所说的“合并”是什么意思以上是C#学习教程:Howtomergetwolistsofdifferentobjects?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: