Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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# 检查列表列表中的元素是否包含其他列表_C#_Arrays_List_Function_Contains - Fatal编程技术网

C# 检查列表列表中的元素是否包含其他列表

C# 检查列表列表中的元素是否包含其他列表,c#,arrays,list,function,contains,C#,Arrays,List,Function,Contains,我有列表的列表或数组。它包含某些有序序列: 0 1 2 3 4 5 6 23 24 25 28 等等 我想添加另一个序列,但前提是它是唯一的(容易),并且任何列表都不包含它。例如: 0 1 2 将被拒绝,函数将返回false,并且 9 10 将被接受,函数将返回true如果我理解正确,您希望搜索列表中的所有列表,查看是否有任何列表包含一组数字,如果没有,则将数字添加为新列表 一种方法是使用Linq: public static void AddListIfNotExist(List<

我有
列表
列表
数组
。它包含某些有序序列:

0 1 2 3
4 5 6
23 24 25 28
等等

我想添加另一个序列,但前提是它是唯一的(容易),并且任何
列表都不包含它。例如:

0 1 2
将被拒绝,函数将返回
false
,并且

9 10

将被接受,函数将返回
true

如果我理解正确,您希望搜索
列表中的所有列表,查看是否有任何列表包含一组数字,如果没有,则将数字添加为新列表

一种方法是使用Linq:

public static void AddListIfNotExist(List<List<int>> lists, List<int> newList)
{
    if (lists == null || newList == null) return;

    if (!lists.Any(list => newList.All(item => list.Contains(item))))
    {
        lists.Add(newList);
    }
}
publicstaticvoidaddlistifnotexist(列表列表,列表新列表)
{
if(lists==null | | newList==null)返回;
如果(!lists.Any(list=>newList.All(item=>list.Contains(item)))
{
列表。添加(newList);
}
}
在使用中,它可能看起来像:

var lists = new List<List<int>> 
{
    new List<int> { 0, 1, 2, 3 },
    new List<int> { 4, 5, 6 },
    new List<int> { 23, 24, 25, 28 }
};

var newList1 = new List<int> { 0, 1, 2 };
var newList2 = new List<int> { 9, 10 };

AddListIfNotExist(lists, newList1);
AddListIfNotExist(lists, newList2);
var列表=新列表
{
新列表{0,1,2,3},
新名单{4,5,6},
新名单{23、24、25、28}
};
var newList1=新列表{0,1,2};
var newList2=新列表{9,10};
AddListIfNotExist(列表,newList1);
AddListIfNotExist(列表,newList2);

您需要从这一点开始,我们恐怕不是代码编写服务。@MathiasR.Jessen谢谢您这位好心的陌生人!问题已解决。我没有将其标记为重复链接,但该重复链接要么回答了您的问题,要么您没有正确解释自己。也许至少可以将您的示例数据显示为
列表
列表
,并显示您尝试的无效内容。如果另一个列表中只存在列表的部分成员,该怎么办(即
{0,1,2,9,10}
)?您只想将唯一编号添加为新列表(
{9,10}
),还是全部添加?请提供更多信息。