14

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:

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

And Use it Like this:

public void UseLambda<T> (IEnumerable<T> source , lambda Expr){

var items= Expr.Compile(source);

foreach(var item in items)
     Console.Writeline(item.ToString());
}

public void Main(){
    List<int> numbers = new List<int> { 10, 24, 9, 87, 193, 12, 7, 2, -45, -2, 9 };
    var expr1 = Where(n => n > 6).OrderBy(n => n % 2 == 0).Select(n => n);
    UseLambda(numbers, expr1);
}

Does any one have an idea about it?

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
Saeid
  • 13,224
  • 32
  • 107
  • 173

5 Answers5

8

Check Func(Of T, TResult) Delegate (MSDN)

using System;

public class LambdaExpression
{
   public static void Main()
   {
       Func<string, string> convert = s => s.ToUpper();

       string name = "Dakota";
       Console.WriteLine(convert(name));   
   }
}

From MSDN

The underlying type of a lambda expression is one of the generic Func delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate. In particular, because many methods of types in the System.Linq namespace have Func(Of T, TResult) parameters, you can pass these methods a lambda expression without explicitly instantiating a Func(Of T, TResult) delegate.

EDIT

Possible solution for your case

static void Main(string[] args) 
{
    List<int> numbers = new List<int> { 10, 24, 9, 87, 193, 12, 7, 2, -45, -2, 9 };
    Func<IEnumerable<int>, IEnumerable<int>> expr = n => n.Where(n1 => n1 > 6).OrderBy(n1 => n1 % 2 == 0).Select(n1 => n1);
    UseLambda<int>(numbers, expr);
}
private static void UseLambda<T>(List<T> numbers, 
                                 Func<IEnumerable<T>, 
                                 IEnumerable<T>> expr) 
{
    var values = expr(numbers);
    foreach (var item in values) {
       Console.WriteLine(item);
    }
}
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
7

If you define your LINQ expressions like this:

Func<IEnumerable<int>, IEnumerable<int>> expr1 =
               l => l.Where(n => n > 6).OrderBy(n => n % 2 == 0).Select(n => n);

Func<IEnumerable<int>, IEnumerable<int>> expr2 = 
               l => l.TakeWhile((n, index) => n >= index);

And your UseLambda method as:

public void UseLambda<T> (IEnumerable<T> source 
                          ,Func<IEnumerable<T>, IEnumerable<T>> 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.

Saeid
  • 13,224
  • 32
  • 107
  • 173
Samuel
  • 1,374
  • 10
  • 16
  • 2
    There is an error in define expr1, expr2: 'System.Collections.IEnumerable' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Collections.IEnumerable' could be found (are you missing a using directive or an assembly reference?) – Saeid Jan 30 '12 at 11:57
1

Do you mean something like this:

public void UseLambda<T> (IEnumerable<T> source , Func<T, bool> where, Func<T, bool> order)
{
    if(source != null)
    {
        IOrderedEnumerable<T> 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);
MonkeyCoder
  • 2,600
  • 2
  • 28
  • 30
0

Well, a lambda is nothing but a delegate, so you could have a method like this:

public void DoIt(IEnumerable a, Action<IEnumerable> performThis)
{
  performThis(a);
}

But where's the sense in it? Instead of calling a method that applies your lambda, why not calling it directly as you do in the last lines of your code?

Krumelur
  • 32,180
  • 27
  • 124
  • 263
0
public void UseLambda<T>(IEnumerable<T> source, Expression<Func<IEnumerable<T>, IEnumerable<T>>> expr)
{
    var items = expr.Compile();

    foreach (var item in items.Invoke(source))
    {
        Console.WriteLine(item.ToString());
    }
}


public void Main()
{
    Expression<Func<IEnumerable<int>, IEnumerable<int>>> expr = s => s.Where(n => n > 6).OrderBy(n => n % 2 == 0).Select(n => n);
    var list = new List<int> { 10, 24, 9, 87, 193, 12, 7, 2, -45, -2, 9 };

    UseLambda(list, expr);
}
Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31