无法在 C# 中将一个列表附加到另一个列表...尝试使用 AddRange

发布于 2024-08-11 04:33:49 字数 628 浏览 5 评论 0原文

您好,我正在尝试将 1 个列表附加到另一个列表。我之前使用 AddRange() 完成了它,但它似乎在这里不起作用...这是代码:

IList<E> resultCollection = ((IRepository<E, C>)this).SelectAll(columnName, maxId - startId + 1, startId);                
IList<E> resultCollection2 = ((IRepository<E, C>)this).SelectAll(columnName, endId - minId + 1, minId);
resultCollection.ToList().AddRange(resultCollection2);

我进行了调试以检查结果,这是我得到的:resultCollection 的计数为 4 resultCollection2 的计数为 6,添加范围后,resultCollection 的计数仍然只有 4,而它应该有数到 10。

有人能看出我做错了什么吗?任何帮助表示赞赏。

谢谢,
马特

Hi I'm trying to append 1 list to another. I've done it using AddRange() before but it doesn't seem to be working here... Here's the code:

IList<E> resultCollection = ((IRepository<E, C>)this).SelectAll(columnName, maxId - startId + 1, startId);                
IList<E> resultCollection2 = ((IRepository<E, C>)this).SelectAll(columnName, endId - minId + 1, minId);
resultCollection.ToList().AddRange(resultCollection2);

I did debugging to check the results, here's what I got: resultCollection has a count of 4 resultCollection2 has a count of 6, and after adding the range, resultCollection still only has a count of 4, when it should have a count of 10.

Can anyone see what I'm doing wrong? Any help is appreciated.

Thanks,
Matt

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

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

发布评论

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

评论(5

短暂陪伴 2024-08-18 04:33:49

当您调用 ToList() 时,您并不是将集合包装在 List 中,而是创建了一个新的 List里面有相同的物品。因此,您在这里实际上要做的是创建一个新列表,向其中添加项目,然后丢弃该列表。

您需要执行以下操作:

List<E> merged = new List<E>();
merged.AddRange(resultCollection);
merged.AddRange(resultCollection2);

或者,如果您使用的是 C# 3.0,只需使用 Concat,例如

resultCollection.Concat(resultCollection2); // and optionally .ToList()

When you call ToList() you aren't wrapping the collection in a List<T> you're creating a new List<T> with the same items in it. So what you're effectively doing here is creating a new list, adding the items to it, and then throwing the list away.

You'd need to do something like:

List<E> merged = new List<E>();
merged.AddRange(resultCollection);
merged.AddRange(resultCollection2);

Alternatively, if you're using C# 3.0, simply use Concat, e.g.

resultCollection.Concat(resultCollection2); // and optionally .ToList()
眼睛会笑 2024-08-18 04:33:49

我假设 .ToList() 正在创建一个新集合。因此,您的物品将被添加到新的收藏中,该收藏会立即被丢弃,而原始物品则保持不变。

I would assume .ToList() is creating a new collection. Therefore your items are being added to a new collection that is immediately thrown away and the original remains untouched.

昨迟人 2024-08-18 04:33:49

resultCollection.ToList() 将返回一个新列表。

尝试:

List<E> list = resultCollection.ToList();
list.AddRange(resultCollection2);

resultCollection.ToList() will return a new list.

Try:

List<E> list = resultCollection.ToList();
list.AddRange(resultCollection2);
影子的影子 2024-08-18 04:33:49

尝试

<罢工>
IList newList = resultCollection.ToList().AddRange(resultCollection2);

List<E> newList = resultCollection.ToList();
newList.AddRange(resultCollection2);

Try


IList newList = resultCollection.ToList().AddRange(resultCollection2);

List<E> newList = resultCollection.ToList();
newList.AddRange(resultCollection2);
客…行舟 2024-08-18 04:33:49

您可以使用以下任意一项:

List<E> list = resultCollection as List<E>;
if (list == null)
    list = new List<E>(resultCollection);
list.AddRange(resultCollection2);

或:

// Edit: this one could be done with LINQ, but there's no reason to limit
//       yourself to .NET 3.5 when this is just as short.
List<E> list = new List<E>(resultCollection);
list.AddRange(resultCollection2);

或:

List<E> list = new List<E>(resultCollection.Concat(resultCollection2));

You can use any of the following:

List<E> list = resultCollection as List<E>;
if (list == null)
    list = new List<E>(resultCollection);
list.AddRange(resultCollection2);

Or:

// Edit: this one could be done with LINQ, but there's no reason to limit
//       yourself to .NET 3.5 when this is just as short.
List<E> list = new List<E>(resultCollection);
list.AddRange(resultCollection2);

Or:

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