繁体   English   中英

C#比较C#中的两个整数列表

[英]c# comparing two lists of integers in c#

我有两个清单是

List<int> comments ;
List<int> no_checks;

现在我想看看是否所有的no_checks都有评论。 因此,在我处理数据的过程中,每当添加评论时,便将其ID添加到评论列表中,而当选择无广播时,便将其ID添加至no_checks

因此向控制台输出的示例

1。

 below should return false;
 comment=[12, 13,15]
 no_checks = [12,13,15,17 ] //one no has no comment

2

 below should return true  
 comment=[12, 13,15]
 no_checks = [12,13 ] //all have comments  

所以被困在这里

public bool allnoHaveComments(){
 var allhave=true;

 for(var i=0; i<no_checks.count; i++){ 
    //if one no_checks is not contained in comments allhave=false

  }
  return allhave;
 }

我如何继续进行比较并检查以查看no_checks中的ID是否全部包含在注释中,否则它们不会返回false

我该怎么办

bool isallnoHaveComments= !no_checks.Except(comment).Any();

这为您提供了no_checks中不在注释中的值

no_checks.Except(comments)

您的问题基本上是检查一个数组是否是另一个数组的子集

我在这里使用了两个for循环,一个用于注释列表,另一个用于no_checks,嵌套在注释内部的循环中

private bool check_Commentexist(List<int> comments, List<int> no_checks)
{
    var j = 0;
    var k = 0;

    Boolean commentexist = false;
    for (var i = 0; i < no_checks.Count; i++)
    {
        j = no_checks[i];
        commentexist = false;
        for (var x = 0; x < comments.Count; x++)
        {
            k = comments[x];
            if (j == k) { commentexist = true; break; }
        }
        //if no match found for atleast on element break the loop
        if (commentexist == false) break;
        //else if match found the looping continues to check the
        //next element in 'no_checks' for a mathcing comment
    }

    return commentexist;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM