Dynamic Linq 2 Sql using Expressions Trees raising exception "Binary Operator LessThan not defined for System.String and System.String"
I'm trying to write a dynamic Linq 2 Sql query using Expressions trees but I'm getting a exception telling me that the LessThan and GreaterThan operators are not defined for System.String
and System.String
, which i find odd, is that true? or am I doing something wrong?
Expression<Func<SomeDataContextType, string>> codeSelectorExpresion =
x => x.CodeColumn;
var row = Expression.Parameter(typeof(SomeDataContextType), "row");
var expression =
Expression.GreaterThan(
Expression.I开发者_运维百科nvoke(codeSelectorExpression, row),
Expression.Constant("someString", typeof(string)));
//I'm trying to build something like SomeDataContextType.CodeColumn > "someString"
Sorry, after strugling for a while i realized that the >
and <
operators on strings are implemented calling to string.CompareTo
, so i updated the code to use the string.CompareTo
method instead and it worked. Thank you anyway,
Tthe expression need to be:
var expression =
Expression.GreaterThan(
Expression.Call(
Expression.Invoke(codeSelectorExpression, row),
typeof(string).GetMethod("CompareTo", new[] {typeof(string)}),
Expression.Constant("someString")),
Expression.Constant(0, typeof(int)));
精彩评论