C# expression tree, property convert string to int/long

4 weeks ago 27
ARTICLE AD BOX

I have an IQueryable expression:

... Where(x => Convert.ToInt64(x.string_param) > Convert.ToInt64(string_const)) ...

How to build this throught expression tree? Kind of:

var left = ...? var right = Expression.Constant(Convert.ToInt64(filter.From), typeof(Int64)); greaterExpression = Expression.GreaterThanOrEqual(left, right);

This line of code:

var left = Expression.Convert(property, typeof(Int64))

is not working, it throws an exception:

No coercion operator is defined between types 'System.String' and 'System.Int64'

This is the method signature:

public static Expression<Func<TQuery, bool>> GetFilterRange<TQuery>(FilterRange<string> filter) { var paramter = Expression.Parameter(typeof(TQuery)); var property = Expression.PropertyOrField(paramter, filter.PropertyName); ... return greaterExpression; }

PS: string is nulable type in query string?

marc_s's user avatar

marc_s

760k186 gold badges1.4k silver badges1.5k bronze badges

Алексей Корсак's user avatar

4

Read Entire Article