[英]Compare List<String[]> With Multiple Conditions
我有兩個List<String[]>
(字符串數組列表),我想將內容與某些條件相互匹配,因此最終結果將返回true為false。
List<string> TestA= {"A001","A002","A003","B001","B001","C003","D000","E001"};
List<string> TestB= {"A001","A002","A003","B999","C003"};
我想為以下條件寫一個函數。
以下是使用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;
}
希望這可以幫助。
不確定我明白了,但檢查一下:
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();
編輯
根據您的進一步說明,我了解您的前綴可能有多個字母,所以我編輯了我的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.