is there a way to set a variable to something at the end of foreach loop if condition not met in C#? -


foreach (objecta in alist()) {     foreach (objectb b in blist)     {         if (a.variable != b.variable1 && a.variable() != b.variable2)         {             a.setvariable("error");         }     } } 

the problem getting goes through foreach loop first time , sets variable error without checking if other values (when goes through loop again) finds match.

what wait until goes through lists , @ last foreach loop iteration if nothing in alist matches variable target && variable source in blist set error flag.

any suggestions around appreciated.

try doing other way around. search match instead of searching non-matches.

foreach (objecta in alist()) {     bool foundmatch = false;     foreach (objectb b in blist)     {         if (a.variable == b.variable1 || a.variable() == b.variable2)         {             foundmatch = true;             break;         }     }     if (!foundmatch)      {         a.setvariable("error");     } } 

Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -