Dev
Dev

Reputation: 295

Compare List<String[]> With Multiple Conditions

I have two List<String[]>'s (string array lists), and I'd like to match the contents with each other with some condition so final result will return true of false.

List<string> TestA= {"A001","A002","A003","B001","B001","C003","D000","E001"};

List<string> TestB= {"A001","A002","A003","B999","C003"};

i would like to write a function for below condition.

  1. if All the items of TestA match with TestB (In TestA same item can be multiple times [ex. B001]) ==> return true
  2. if TestB conatins any item having digit 999 [Ex B999] then no need to loop for items start with B in testA (this set bool true) and loop of TestA start from C003 [in this case i think we need to remove all Items of B from ListA if ListB conats B999]. Continue.. so loop run for TestA item C003. this matched with item in TestB again set true Now for D000 not match item in ListB now finally bool set to false and break.

Upvotes: 0

Views: 1240

Answers (2)

Wasp
Wasp

Reputation: 3425

Not sure I understood, but check this:

var filter = 
    from b in TestB
    where b.Contains("999")
    select b.Replace("999", "");

var cleaned = 
    from a in TestA
    where !filter.Any(f => a.StartsWith(f))
    select a;

var check = cleaned.Distinct()
                   .Except(TestB).Any();

EDIT

From your further specification I understand your prefixes might have more than one letter, so I edited my answer.

Upvotes: 1

Aviran Cohen
Aviran Cohen

Reputation: 5691

Here's all you need using LINQ:

// Condition 2:
// Get the characters of in list B that contains the "999" string. 
var badOnes = ListB.Where(s=>s.Contains("999").Select(s=>s[0])

// If there is any forbidden characters remove them from List A
if (badOnes.Length > 0)
{
    ListA.RemoveAll(x => x[0] == badOnes.Exists(c => c == x));
}

// Condition 1:
if (ListA.Distinct().Intersect(ListB).Length == ListA.Distinct().Length)
{
    return true;
}

Hope this helps.

Upvotes: 1

Related Questions