TheNerd
TheNerd

Reputation: 339

How to pass a System.Linq.LambdaExpression?

I have a method like this:

public List<MyObjects> All<TEntity>(params LambdaExpression[] exprs)

with the intention that I can call it like this:

All<SomeObject>(a => a.Collection1, a=> a.Collection2, a=>a.Collection3);

However, my method signature does not appear to take the expression correctly. What am I doing wrong? How would I write the method signature to get the desired effect?

edited: I realized that my example method call wasn't accurately reflecting what I was trying to do in real life :)

thanks!!

Upvotes: 2

Views: 108

Answers (2)

Paolo Moretti
Paolo Moretti

Reputation: 56024

Perhaps the cleanest way in this case would be to write an extension method.

public static class MyExtensions
{
    public static List<TEntity> All<TEntity, TResult>(
        this TEntity entity,
        params Func<TEntity, TResult>[] exprs)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        if (exprs == null)
        {
            throw new ArgumentNullException("exprs");
        }

        // TODO: Implementation required
        throw new NotImplementedException();
    }
}

Note that you don't have to specify type arguments when you're calling the method because of the type inference.

class C
{
    public List<string> Collection1 {get; set;}
    public List<string> Collection2 {get; set;}
    public List<string> Collection3 {get; set;}
    // ...
}
// ...
var c = new C();            
c.All(x => x.Collection1, x => x.Collection2, x => x.Collection3);

Upvotes: 1

Desty
Desty

Reputation: 2775

Did you meant something like

public List<MyObjects> All(params Action<ICollection>[] exprs)

All(a => new List<int>(), b => new List<string>(), c => new List<bool>()); 

Upvotes: 1

Related Questions