c# - TrimStart in Expression Tree with LINQ to Entities -
i'm attempting write expression tree can allow dynamic use of startswith()
method on non-string valued columns using entity framework.
e.g. integervaluedcolumn.startswith(5)
return 500
, 5000
, 555
, 5123
, etc
i trying write expression tree based on answer:
how query integer column "starts with" in entity framework?
here have far:
methodinfo stringconvert = typeof(sqlfunctions).getmethod("stringconvert", new[] { typeof(double?) }); expression castexpression = expression.convert(propertyexpression, typeof(double?)); expression convertexpression = expression.call(null, stringconvert, castexpression); methodinfo trimstart = typeof(string).getmethod("trimstart"); expression nullexpression = expression.constant(null, typeof(char[])); expression trimexpression = expression.call(convertexpression, trimstart, nullexpression); methodinfo startswith = typeof(string).getmethod("startswith", new[] { typeof(string) }); expression methodexpression = expression.call(trimexpression, startswith, constantexpression); return methodexpression;
when compile , run expression, following exception:
the method 'system.string trimstart(char[])' supported in linq entities when there no trim characters specified arguments.
in original example, expression is:
sqlfunctions.stringconvert((double)x.accountnumber) .trimstart().startswith(searchterm)
but comes out as:
stringconvert(convert(x.accountnumber)).trimstart(null).startswith(searchterm)
i have removed 2 lines dealing trimstart (nullexpression , trimexpression) , verified statement runs (excluding thought error being caused different language use). theory based on exception message trimstart() method wants called 0 parameters, when try expression builder tells me incorrect number of parameters have been passed in figure i'm missing something.
how go calling trimstart method trimstart()
instead of trimstart(null)
or trimstart(new char[0])
using expression trees?
Comments
Post a Comment