清单<名单,LT; INT>> remove()方法 [英] List<List<int>> Remove() method

查看:190
本文介绍了清单<名单,LT; INT>> remove()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对列表的列表中删除()方法,但它不是为我工作结果
简单的例子应该说一切:

I'd like to use Remove() method on list of lists, but it's not working for me.
Simple example should say everything:

List<List<int>> list = new List<List<int>>();
list.Add(new List<int> { 0, 1, 2 });
list.Add(new List<int> { 1, 2 });
list.Add(new List<int> { 4 });
list.Add(new List<int> { 0, 1, });

list.Remove(new List<int> { 1, 2 });

如果我使用RemoveAt移除(1)它工作正常,但删除()不结果$ B。 $ b这显然是同样的原因,这段代码返回false:

If I use RemoveAt(1) it works fine but Remove() not.
It is obviously the same reason that this code returns false:

List<int> l1 = new List<int>();
List<int> l2 = new List<int>();
l1.Add(1);
l2.Add(1);

bool b1 = l1 == l2; // returns False
bool b2 = l1.Equals(l2); // returns False too



所以,在我看来,我不能简单地比较两个列表,甚至数组。我可以使用循环,而不是remove()方法,但必须有更简单的方法。

So it seems to me that I cannot simply compare two lists or even arrays. I can use loops instead of Remove(), but there must be easier way.

先谢谢了。

推荐答案

第一种方法:

List<int> listToRemove = new List<int> { 1, 2 };
list.RemoveAll(innerList => innerList.Except(listToRemove).Count() == 0);

这还删除列表{2,1}

This also removes the List { 2, 1 }

第二条本办法(首选):

List<int> listToRemove = new List<int> { 1, 2 };
list.RemoveAll(innerList => innerList.SequenceEqual(listToRemove));

这将删除包含相同的序列提供的列表中的所有名单。

This removes all lists that contain the same sequence as the provided list.

这篇关于清单&LT;名单,LT; INT&GT;&GT; remove()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆