I have two table sets:
One Entity (values "-1" to "-7") & One Static Data (values "-8" to "-11").
The Static Data tables rely on Entity tables to validate successfully. I am trying to check if Static Data tables exist and Entity tables do not, and if they do then append an exception onto the exception StringBuilder.
The following code does the job and works as hoped, however, it is an eyesore and I believe there must be a better way out there to do it. I have had a look on Google, however none that I found seem to be related to this specific scenario.
//Check Entities (Company, Trust, Individual) exist to link static data (Address, Email, Bank Account, Phone Number) to, if not add exception
if ((!imports.Tables.Contains("-1") && !imports.Tables.Contains("-2") && !imports.Tables.Contains("-3") &&
!imports.Tables.Contains("-4") && !imports.Tables.Contains("-5") && !imports.Tables.Contains("-6") &&
!imports.Tables.Contains("-7")) && imports.Tables.Contains("-8") && imports.Tables.Contains("-9") &&
imports.Tables.Contains("-10") && imports.Tables.Contains("-11"))
{
exception.Append("No Entity Found: Address, Email, Bank Account & Phone Number require an Entity (Trust, Company, Individual) to be created to pass validation.");
exception.AppendLine();
}
I tried to add the values to a list and do a comparison on the list but was unsuccessful, below is what I mustered up for one of the tables:
List<string> staticData = new List<string>{ staticData.Add("-8"); staticData.Add("-9"); staticData.Add("-10"); staticData.Add("-11")}
if((imports.Tables.Contains(staticData) {} // Didn't expect this to work without looping through the list but that wouldn't be ideal in an if statement.
Thought I'd come on here to seek help.
Create two lists and use .All()
to test, if numbers in Tables
not exist in staticData1
and exist in staticData2
, like the following code :
List<string> staticData1 = new List<string>
{
"-1","-2","-3","-4","-5","-6","-7"
};
List<string> staticData2 = new List<string>
{
"-8","-9","-10","-11"
};
if (imports.Tables != null
&& imports.Tables.Count > 0
&& staticData1.All(x => !Tables.Contains(x))
&& staticData2.All(x => Tables.Contains(x)))
{
exception.Append("No Entity Found: Address, Email, Bank Account & Phone Number require an Entity (Trust, Company, Individual) to be created to pass validation.");
exception.AppendLine();
}
I hope you find this helpful.