Dynamic Linq 2 Sql using expressions Trees, ascending exception "Binary LessThan operator not defined for System.String and System.String"

I'm trying to write a dynamic Linq 2 Sql query using expression trees, but I get an exception telling me that the LessThan and GreaterThan operators are not defined for System.Stringand System.Stringwhich I find strange, is this 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.Invoke(codeSelectorExpression, row),
       Expression.Constant("someString", typeof(string)));
//I'm trying to build something like SomeDataContextType.CodeColumn > "someString"
+3
source share
1 answer

, , > < string.CompareTo, , string.CompareTo, . , , :

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)));
+4

Source: https://habr.com/ru/post/1728394/


All Articles