tags:

views:

142

answers:

1

How would you translate the following generic Lambda function into a lambda expression :

context.AssociateWith<Product>(p => p.Regions.Where(r => r.Country == 'Canada')

I'm trying to create a full lambda expression without any <T> or direct call. Something like :

void AddFilter(ITable table, MetaDataMember relation)
{
    var tableParam = Expression.Parameter(table.ElementType, "e");
    var prop = Expression.Property(tableParam, relation.Name);
    var func = typeof(Func<,>).MakeGenericType(table.ElementType, relation.type)
    var exp = Expression.Lambda(func, prop, tableParam);
}

This will produce e.Regions... but I'm unable to get the Where part from there...

A: 

Try this, it's not pretty but it gives you a valid expression for the whole structure. You could define the inner lambda as an expression but you would still have to compile it before you could pass it to Where(), so for the purposes of this answer it seems redundant.

Expression<Func<Product, IEnumerable<Region>>> getRegions = 
      p => p.Regions.Where(r => r.Country == "Canada");
Sam
I'm more looking into something that would use non typed parameters. I'll precise my question more. Thanx tho!
Mathlec