我有两个List<String[]>
(字符串数组列表),我希望在某些条件下将内容相互匹配,以便最终结果返回true为假。
List<string> TestA= {"A001","A002","A003","B001","B001","C003","D000","E001"};
List<string> TestB= {"A001","A002","A003","B999","C003"};
我想为以下条件写一个函数。
答案 0 :(得分:1)
以下是使用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;
}
希望这有帮助。
答案 1 :(得分:1)
不确定我明白了,但请检查一下:
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();
修改强>
根据您的进一步说明,我了解您的前缀可能有多个字母,因此我编辑了我的答案。