如何从多个List<>中获取所有元素?

发布于 2024-12-05 14:47:46 字数 460 浏览 0 评论 0 原文

在 C# 中,我有以下类型:

List<List<mytype>> MyLists;
List<mytype> MainList;

我想获取所有 List<> 中的每个元素放在 MyList 中并将它们放入 MainList 中。然后,MainList 将仅具有由每个 List<> 内的所有元素组成的元素。我的列表。我已尝试以下操作,但收到有关无法推断类型的错误:

MyLists.ForEach(list => MainList.AddRange(list.SelectMany(x => x != null)));

我不确定要在 SelectMany() 中放入什么,因为我想要 List<> 中的所有元素。这些元素不需要满足任何标准。

有什么建议可以如何做到吗?

In C#, I have the following types:

List<List<mytype>> MyLists;
List<mytype> MainList;

I would like to take each element in all List<> inside MyList and put them in MainList. MainList will then have only elements composed of all elements that were inside each List<> of MyList. I've tried the following but get an error about not being able to infer the type:

MyLists.ForEach(list => MainList.AddRange(list.SelectMany(x => x != null)));

I wasn't sure what to put in SelectMany() since I want all elements within the List<>. Those elements don't need to meet any criteria.

Any suggestions how it can be done?

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

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

发布评论

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

评论(3

站稳脚跟 2024-12-12 14:47:46
MainList.AddRange(from list in MyLists
                  from element in list
                  select element);

或者,如果你愿意的话,

MainList.AddRange(MyLists.SelectMany(list => list));
MainList.AddRange(from list in MyLists
                  from element in list
                  select element);

Or, if you prefer,

MainList.AddRange(MyLists.SelectMany(list => list));
糖粟与秋泊 2024-12-12 14:47:46

这只需要一个 SelectMany 调用:

MainList = MyLists.SelectMany(l => l).ToList();

请注意,这不需要在此调用之前构造/初始化 MainList,因为它是通过 ToList() 调用完全初始化的。


编辑:

由于您确实包含了 null 检查,如果您需要从列表中删除 null 元素,您也可以添加该检查:

MainList = MyLists.SelectMany(l => l.Where(i => i != null)).ToList();

和/或过滤器整个 null 列表:

MainList = MyLists
             .Where(l => l != null)
             .SelectMany(l => l.Where(i => i != null))
             .ToList();

另外,如果您想添加项到您的 MainList 中,而不是创建 MainList "原始列表中只有元素”,您可以使用AddRange 仍然:

MainList.AddRange(MyLists.SelectMany(l => l));

This just requires a single SelectMany call:

MainList = MyLists.SelectMany(l => l).ToList();

Note that this doesn't require constructing/initializing MainList prior to this call, as it's completely initialized from the ToList() call.


Edit:

Since you did include a null check, if you need to remove null elements from within your list, you could add that check, as well:

MainList = MyLists.SelectMany(l => l.Where(i => i != null)).ToList();

And/Or filter for entire null lists:

MainList = MyLists
             .Where(l => l != null)
             .SelectMany(l => l.Where(i => i != null))
             .ToList();

Also, if you want to add items to your MainList, as opposed to making MainList "have only elements" in the original lists, you could use AddRange still:

MainList.AddRange(MyLists.SelectMany(l => l));
讽刺将军 2024-12-12 14:47:46

另一种选择,更接近您最初的尝试: MyLists.ForEach(list => MainList.AddRange(list));

换句话说,如果您使用 ForEach,则不需要 SelectMany 甚至 Select和添加范围。

Another option, closer to your original attempt: MyLists.ForEach(list => MainList.AddRange(list));

In other words, you don't need SelectMany or even Select if you are using ForEach and AddRange.

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