通过在单独列表中分割标准(如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)
因此,而不是过滤,你想得到所有的元素与一个额外的领域提及真/假和理由? –