Concatenating Lambda Functions in C#

前端 未结 6 1963
臣服心动
臣服心动 2020-12-23 14:46

Using C# 3.5 I wanted to build up a predicate to send to a where clause piece by piece. I have created a very simple Console Application to illustrate the solution that I a

6条回答
  •  独厮守ぢ
    2020-12-23 15:06

    If you want to combine predicates, try this;

    public static Predicate Combine(params Predicate[] predicates)
    {
    return t => predicates.All(pr => pr(t));
    }
    

    You call it like this;

    Predicate shortAndSweet = Combine
    (
        s => s.Length < 10,  // short,
        s => s == "sweet"    // and sweet
    );
    

提交回复
热议问题