1

I have two lists that is

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

Now i would like to see if all no_checks have comments. So during my processing of data whenever a comment is added am adding its id to comment list and when a no radio is selected am adding its id to no_checks

So a sample output to console has

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  

So am stuck here

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;
 }

How can i proceed to compare and check to see that id's in no_checks are all contained in comments else if they are not return false

How can i go about this

  • Does order matter? If not, then you might consider using `HashSet` instead of `List`. A hashset represents a set of unordered things, and directly supports operations like the one you want. – Eric Lippert Jul 24 '17 at 18:01

2 Answers2

3
bool isallnoHaveComments= !no_checks.Except(comment).Any();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

This gives you the values in no_checks that aren't in comments

no_checks.Except(comments)

Your problem is basically Check whether an array is a subset of another.

Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75