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

后端 未结 5 1240
-上瘾入骨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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 08:10

    If you define your LINQ expressions like this:

    Func, IEnumerable> expr1 =
                   l => l.Where(n => n > 6).OrderBy(n => n % 2 == 0).Select(n => n);
    
    Func, IEnumerable> expr2 = 
                   l => l.TakeWhile((n, index) => n >= index);
    

    And your UseLambda method as:

    public void UseLambda (IEnumerable source 
                              ,Func, IEnumerable> lambda)
    {
        var items= lambda(source);
    
        foreach(var item in items)
           Console.Writeline(item.ToString());
        }
    }
    

    Then you I think you have what you're looking for.

提交回复
热议问题