C# 两个列表的 linq 联合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12999878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:16:38  来源:igfitidea点击:

linq union of two lists

c#linq

提问by frenchie

I have an object model that contains a list of longs. I want to get the combined list of longs of two different instances. When I write this:

我有一个包含长列表的对象模型。我想获得两个不同实例的多头组合列表。当我写这个:

var MyCombinedList = TheObject1.ListOfLongs.Union(TheObject2.ListOfLongs);

I get an empty list. Note that sometimes TheObject2can have an empty list.

我得到一个空列表。请注意,有时TheObject2可以有一个空列表。

What am I doing wrong?

我究竟做错了什么?

Thanks.

谢谢。

采纳答案by Kundan Singh Chouhan

Use Concat()this will concatenates two sequences. So try this instead :

使用Concat()这将连接两个序列。所以试试这个:

var MyCombinedList = TheObject1.ListOfLongs.Concat(TheObject2.ListOfLongs);

Good Luck !!

祝你好运 !!

回答by John Woo

how about using Concat

怎么样使用 Concat

var list = TheObject1.ListOfLongs.Concat(TheObject2.ListOfLongs).ToList();

or

或者

var list = TheObject1.ListOfLongs.Union(TheObject2.ListOfLongs).ToList();