Elsid Elsid - 4 months ago 7
SQL Question

Iterate through list boolean print out what failed C#

I am doing an assertion to find some sql fields from what database to another.

bool ContainsBracketsWithString = AssertionValue.All(a =>
CredentialTypeDescription.Any(b =>
a.Field1 == b.Field1
)
);

Assert.False(ContainsBracketsWithString);


This works fine on telling me if it passes or fails, but when it fails I want to know which values didn't match.

Can somebody provide some code on how to do this?

Answer

You could do both in one pass

string message = String.Empty;
bool ContainsBracketsWithString = AssertionValue.All(a =>
             CredentialTypeDescription.Any(b => {
                     var passed = a.Field1 == b.Field1;
                     if(!passed && String.IsNullOrEmpty(message)){
                        message = "Some message to identify which failed"; 
                     }
                     return passed;
                 })
               );

Assert.False(ContainsBracketsWithString, message);