通過在單獨列表中分割標準(如Func<string,bool
s),您可以測試每個單獨的設置。一步,通過纏繞它們作爲Expression<>
中,(失敗)繞圈的內容可以得到:
List<string> temp = new List<string>{"bla bla", "111","222","1111111"};
var criteria = new Expression<Func<string,bool>>[]{x => x.Length <= 5 , x => x.Contains("1")}
.Select(c=>new{Test=c.Compile(), Name = c.ToString()}).ToList(); //get precompiled lambdas with their names, based on the lambda expressions
var results = from s in temp
let fail = criteria.FirstOrDefault(c=>!c.Test(s)) //get the first criterium to fail (if any)
select new {Value = s, Result = fail ==null , FailedOn = fail?.Name};
foreach(var m in results) //test output
Console.WriteLine("Value: {0} [{1}] {2}" , m.Value, m.Result, m.Result ? null : " crashed on " + m.FailedOn);
以上的結果:
- 值:血乳酸血乳酸[FALSE]墜毀X =>(x.Length < = 5)
- 值:111 [TRUE]
- 值:222 [FALSE]墜毀在X => x.Contains( 「1」)
- 值:1111111 [假]在x =>(x.Len gth < = 5)
因此,而不是過濾,你想得到所有的元素與一個額外的領域提及真/假和理由? –