Reputation: 295
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.
Upvotes: 0
Views: 1240
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
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