如何合并列表>在 C# 中

发布于 2024-10-09 05:27:30 字数 392 浏览 1 评论 0 原文

我有一个 List>,其中包含

{  {"A" , "B" }, 
   {"C" , "D" }
}

我需要将所有内部列表合并到另一个列表中,

因此生成的 List 将包含

     {"A","B","C","D"}

现在我使用for循环来执行此操作

有没有办法使用LINQLambda表达式来执行此操作。

请帮助我做到这一点。

提前致谢。

I'm having a List<List<String>>, and which contains

{  {"A" , "B" }, 
   {"C" , "D" }
}

I need to union all the innerlist into another list

So the resulting List<String> will contain

     {"A","B","C","D"}

Now im using for loop to do this

Is there any way to do this Using LINQ or Lambda Expression.

Please help me to do this.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

謸气贵蔟 2024-10-16 05:27:30

不完全是一个联盟,但你可以尝试这个

YourList.SelectMany(l=>l).Distinct()

Not Exactly a Union, but you can try this

YourList.SelectMany(l=>l).Distinct()
始终不够 2024-10-16 05:27:30
List<List<string>> collections = new List<List<string>>()
        {
          new List<string>(){"A" , "B" }, 
          new List<string>() {"C" , "D" }
        };

var list = collections.SelectMany(x => x).ToList();

SelectMany 构建一个表达式树,在计算时将列表列表展平为组合成员的单个列表。

ToList 强制计算表达式树并生成一个列表。

如果要消除重复项,可以在调用“ToList()”之前添加 Distinct 调用

List<List<string>> collections = new List<List<string>>()
        {
          new List<string>(){"A" , "B" }, 
          new List<string>() {"C" , "D" }
        };

var list = collections.SelectMany(x => x).ToList();

SelectMany builds up a expression tree that when evaluated flattens the list of list to a single list of combined members.

ToList forces the expression tree to be evaluated and which results in a List.

If you want to eliminate duplicates you can add a Distinct call before the call to 'ToList()'

浮萍、无处依 2024-10-16 05:27:30

您可以使用 SelectMany 扩展方法。

List<List<String>> masterList = { {"A" , "B" }, {"C" , "D" } };

IEnumerable<string> results = masterList.SelectMany(l => l);

You can use the SelectMany extension method.

List<List<String>> masterList = { {"A" , "B" }, {"C" , "D" } };

IEnumerable<string> results = masterList.SelectMany(l => l);
哆兒滾 2024-10-16 05:27:30
var result = myLists.SelectMany(l => l);
var result = myLists.SelectMany(l => l);
浊酒尽余欢 2024-10-16 05:27:30

聚合怎么样?

myLists.Aggregate((left, right) => left.Union(right));

对我来说,这比使用 SelectMany 更具表现力,因为它准确地告诉您在做什么:通过对所有列表调用 union 来聚合列表列表。

How about Aggregate?

myLists.Aggregate((left, right) => left.Union(right));

To me, this is more expressive than using SelectMany, because it is telling you exactly what you are doing: Aggregate your list of lists by calling union on them all.

为你拒绝所有暧昧 2024-10-16 05:27:30

只是为了好玩:

(from list in theList from e in list select e).Distinct().ToList()

这当然与 @Alexander Taran 的解决方案相同,只是使用查询语法而不是 lambda 语法。 (或者至少应该是 – 我手边没有 LINQPad。)

Just for kicks:

(from list in theList from e in list select e).Distinct().ToList()

This is of course the same solution as @Alexander Taran's, just with query syntax instead of lambda syntax. (Or at least it should be – I don't have my LINQPad handy.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文