views:

35

answers:

1

I am trying to create the following Where clause expression dynamically:

context.Cars.
Where(c => EntityFunctions.DiffDays(c.Created, c.Created) == null).
ToList()

This is the code I am using to create the expression:

var parameter = Expression.Parameter(typeof(Car), "c");
var property = Expression.Property(parameter, "Created");
var function = Expression.Call(typeof(EntityFunctions), "DiffDays", 
    null, property, property);
var comparison = Expression.Equal(function, Expression.Constant(null));
var result = Expression.Lamda<Func<Car, bool>>(comparison, parameter);

Result shows (it seems to be missing the "EntityFunctions.DiffDays"):

{c => (DiffDays(c.Created, c.Created) == null)}

When I try to execute with:

context.Cars.Where(result.Compilte()).ToList()

I get the error message:

this function can only be invoked from linq to entities

Do you know what I am missing? Is it because it is only showing "DateDiff" and not "EntityFunctions.DateDiff"

Thanks. Adam

A: 

Hey Adam,

Remove the "Compile()" call on your last line, like this:

context.Cars.Where(result).ToList();

and defer the compile to the LinqToEntities.

Sleiman