Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查列表是否<;列表<;int>&燃气轮机;包含一个列表<;int>;_C#_List - Fatal编程技术网

C# 如何检查列表是否<;列表<;int>&燃气轮机;包含一个列表<;int>;

C# 如何检查列表是否<;列表<;int>&燃气轮机;包含一个列表<;int>;,c#,list,C#,List,我有一个列表,我想在其中插入一个新的列表。我想在添加新的列表之前检查它是否已经存在于列表中 例如 List<List<int>> MasterList = new List<List<int>>(); List<int> childList = new List<int>(); 但没有任何帮助 e、 g 但当我看到1,2,3或4,5再次出现时,我不想输入,因为它们已经在主列表中的某个位置了您可以使用Linq的Sequenc

我有一个
列表
,我想在其中插入一个新的
列表
。我想在添加新的
列表之前检查它是否已经存在于
列表中

例如

List<List<int>> MasterList = new List<List<int>>();
List<int> childList = new List<int>();
但没有任何帮助

e、 g


但当我看到1,2,3或4,5再次出现时,我不想输入,因为它们已经在主列表中的某个位置了

您可以使用Linq的SequenceEqual

if (MasterList.Any(c => c.SequenceEqual(childList)))
{
    //contains

}

您可以使用Linq的SequenceEqual

if (MasterList.Any(c => c.SequenceEqual(childList)))
{
    //contains

}
List MasterList=newlist();
List childList1=新列表(){1,2,3};
List childList2=新列表(){4,5};
主列表。添加(子列表1);
//主列表。添加(子列表2);
Console.WriteLine(MasterList.Contains(childList1));
Console.WriteLine(MasterList.Contains(childList2));
Console.WriteLine(主列表[0]。包含(1));
Console.WriteLine(主列表[0]。包含(7));
//真的
//假的
//真的
//假的
也许我不明白你的问题是什么,对不起。

List MasterList=new List();
List childList1=新列表(){1,2,3};
List childList2=新列表(){4,5};
主列表。添加(子列表1);
//主列表。添加(子列表2);
Console.WriteLine(MasterList.Contains(childList1));
Console.WriteLine(MasterList.Contains(childList2));
Console.WriteLine(主列表[0]。包含(1));
Console.WriteLine(主列表[0]。包含(7));
//真的
//假的
//真的
//假的

也许我不明白你的问题是什么,对不起。

你可以这样比较两个列表:

bool AreListsIdentical(List<int> lhs, List<int> rhs)
{
    if(lhs.Count != rhs.Count) return false;

    return lhs.Zip(rhs, (l,r) => l == r).All(value => value);
}
bool AreListsIdentical(列表左、列表右)
{
如果(lhs.Count!=rhs.Count)返回false;
返回lhs.Zip(rhs,(l,r)=>l==r.All(value=>value);
}
现在,使用此函数,您可以使用Linq函数搜索匹配列表:

List<List<int>> masterList = new List<List<int>>();
List<int> childList = new List<int>(){1, 2, 3};
masterList.Add(childList);

var listToSearchFor = new List<int>{1,2,3};
masterList.Any(list => AreListsIdentical(list, listToSearchFor));
List masterList=newlist();
List childList=newlist(){1,2,3};
主列表。添加(子列表);
var listToSearchFor=新列表{1,2,3};
masterList.Any(list=>AreListsIdentical(list,listToSearchFor));

您可以比较两个列表是否相等,如下所示:

bool AreListsIdentical(List<int> lhs, List<int> rhs)
{
    if(lhs.Count != rhs.Count) return false;

    return lhs.Zip(rhs, (l,r) => l == r).All(value => value);
}
bool AreListsIdentical(列表左、列表右)
{
如果(lhs.Count!=rhs.Count)返回false;
返回lhs.Zip(rhs,(l,r)=>l==r.All(value=>value);
}
现在,使用此函数,您可以使用Linq函数搜索匹配列表:

List<List<int>> masterList = new List<List<int>>();
List<int> childList = new List<int>(){1, 2, 3};
masterList.Add(childList);

var listToSearchFor = new List<int>{1,2,3};
masterList.Any(list => AreListsIdentical(list, listToSearchFor));
List masterList=newlist();
List childList=newlist(){1,2,3};
主列表。添加(子列表);
var listToSearchFor=新列表{1,2,3};
masterList.Any(list=>AreListsIdentical(list,listToSearchFor));

我测试了以下各项,它可以正常工作:

           List<List<int>> masterList = new List<List<int>>() {
                new List<int>() { 1, 2, 3},
                new List<int>() { 1, 2},
                new List<int>() { 1, 2, 3, 4},
                new List<int>() { 1, 2, 5},
                new List<int>() { 1, 3, 7},
                new List<int>() { 2, 3, 4},
                new List<int>() { 1, 5, 8},
                new List<int>() { 1, 4, 9}
            };

            List<int> newList = new List<int>() { 1,2,5};
            Boolean contains = masterList.Any(x => (x.Count() == newList.Count()) && (x.Select((y, i) => y == newList[i]).All(y => y)));
List masterList=新列表(){
新列表(){1,2,3},
新列表(){1,2},
新列表(){1,2,3,4},
新列表(){1,2,5},
新列表(){1,3,7},
新列表(){2,3,4},
新列表(){1,5,8},
新列表(){1,4,9}
};
List newList=newList(){1,2,5};
Boolean contains=masterList.Any(x=>(x.Count()==newList.Count())&(x.Select((y,i)=>y==newList[i])。All(y=>y));

我测试了以下各项,它可以正常工作:

           List<List<int>> masterList = new List<List<int>>() {
                new List<int>() { 1, 2, 3},
                new List<int>() { 1, 2},
                new List<int>() { 1, 2, 3, 4},
                new List<int>() { 1, 2, 5},
                new List<int>() { 1, 3, 7},
                new List<int>() { 2, 3, 4},
                new List<int>() { 1, 5, 8},
                new List<int>() { 1, 4, 9}
            };

            List<int> newList = new List<int>() { 1,2,5};
            Boolean contains = masterList.Any(x => (x.Count() == newList.Count()) && (x.Select((y, i) => y == newList[i]).All(y => y)));
List masterList=新列表(){
新列表(){1,2,3},
新列表(){1,2},
新列表(){1,2,3,4},
新列表(){1,2,5},
新列表(){1,3,7},
新列表(){2,3,4},
新列表(){1,5,8},
新列表(){1,4,9}
};
List newList=newList(){1,2,5};
Boolean contains=masterList.Any(x=>(x.Count()==newList.Count())&(x.Select((y,i)=>y==newList[i])。All(y=>y));
这样:

List<List<int>> listOfIntLists = new List<List<int>>() {
    new List<int>(){ 1,2 },
    new List<int>(){ 3,4 },
    new List<int>(){ 5,6 }
};

List<int> integers = new List<int>() { 1, 2 };

if(listOfIntLists.Any(x => x.All(y => integers.Any(z => z == y))) == false)
{
    listOfIntLists.Add(integers);
}
List listofInList=新列表(){
新列表(){1,2},
新列表(){3,4},
新列表(){5,6}
};
列表整数=新列表(){1,2};
if(listofInList.Any(x=>x.All(y=>integers.Any(z=>z==y)))==false)
{
添加(整数);
}
这样:

List<List<int>> listOfIntLists = new List<List<int>>() {
    new List<int>(){ 1,2 },
    new List<int>(){ 3,4 },
    new List<int>(){ 5,6 }
};

List<int> integers = new List<int>() { 1, 2 };

if(listOfIntLists.Any(x => x.All(y => integers.Any(z => z == y))) == false)
{
    listOfIntLists.Add(integers);
}
List listofInList=新列表(){
新列表(){1,2},
新列表(){3,4},
新列表(){5,6}
};
列表整数=新列表(){1,2};
if(listofInList.Any(x=>x.All(y=>integers.Any(z=>z==y)))==false)
{
添加(整数);
}

您是否尝试了全部而不是任何。如果您正在测试不匹配,请使用any。您是否假设两个列表的顺序相同?如果
MasterList
包含
4,5
的列表,并且您尝试添加
5,4
的列表,该怎么办?“这应该被认为是已经存在的名单吗?”佐哈珀莱德说,“是的。4,5和5,4在我的场景中是一样的。我给了你解决方案。你试过全部而不是任何一个了吗。如果您正在测试不匹配,请使用any。您是否假设两个列表的顺序相同?如果
MasterList
包含
4,5
的列表,并且您尝试添加
5,4
的列表,该怎么办?“这应该被认为是已经存在的名单吗?”佐哈珀莱德说,“是的。4,5和5,4在我的场景中是相同的,我给了你解决方案,OP面临的问题是如果你有
List childList3=new List(){1,2,3}
,然后
MasterList.Contains(childList3)
将是
false
,即使
childList1
的元素是相同的。啊,但很明显,因为如果有两个具有相同内容的列表,那么它是不同的对象,具有不同的地址。这意味着问题是如何检查两个列表对象的内容)是的,我相信这是OP面临的问题,如果你有
list childList3=new list(){1,2,3}
,然后
MasterList.Contains(childList3)
将是
false
,即使
childList1
的元素是相同的。啊,但是很明显,因为如果