How do you divide property with linq expression

Imagine I got an entity:

Nullable<Int64> MyProperty


I would like to do something like this:

Ctx.MyEntity.Where(x=>x.MyProperty/16 == 10) 


with Linq.Expression

So, I create a parameter:

var param = Expression.parameter(typeOf(MyEntity));


Then the property:

var prop = Expression.PropertyOrField(param,"MyProperty");


If I wanted to compare with 10, I would do something like this:

var cmp = Expression.equal(prop,Expression.constant(10,prop.Type));


But first I need to divide the prop by 16.

So I am trying:

var div = Expression.Divide(prop,Expression.constant(16, prop.type);


And that excludes the exclusive type.

Can anyone please help?



source to share

1 answer

You need to create a split expression like this:

Expression.Divide(prop, Expression.Constant((Nullable<Int64>)16, prop.Type))


You must explicitly convert 16 to the type of the property, because when used as a literal, it has a type Int32

that is obviously not the same as Nullable<Int64>




All Articles