Is there any way to pass the lambda expression as a variable or argument?

后端 未结 5 1239
-上瘾入骨i
-上瘾入骨i 2021-02-19 07:10

I need to pass the lambda query as a parameter, the followings code is sample and I am interesting to find an implement for it, there is samples: some thing like this:

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 07:48

    Do you mean something like this:

    public void UseLambda (IEnumerable source , Func where, Func order)
    {
        if(source != null)
        {
            IOrderedEnumerable orderBy = source.Where(where).OrderBy(order);
            foreach (T value in orderBy)
            {
                Console.WriteLine(value);
            }
        }
    }
    

    So that you could call it like so:

    UseLambda(numbers, x => x > 6, x => x % 2 == 0);
    

提交回复
热议问题