Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework string datatype using >,>=,<,<= operator : Lambda expression

Tags:

lambda

c#-4.0

I am working on a dynamic Lambda expression query, without using an API.

If the user selects the FieldName as "AddressLine1" and Operator as >= FieldValue as "K" It should return the results as All the AddressLine1 Field values which starts with K and L,M till Z series.

Here is the code, it works for integer datatypes:

public static Expression CreateBinaryExpression(Expression argLeft, Expression argRight, operatorType opType) {
    switch ((operatorType)opType) {    
        case operatorType.Greater:
            return Expression.GreaterThan(argLeft, argRight);
        case operatorType.GreaterEqual:
            return Expression.GreaterThanOrEqual(argLeft, argRight);
        ...
    }
}

How to modify the code to work with String datatype for Greater than Equal operator. I am looking for expression for this. Anybody have ideas?

like image 685
sivaL Avatar asked Apr 17 '26 10:04

sivaL


2 Answers

You can use the String.Compare() method:

return String.Compare(argLeft, argRight) >= 0;

Compare() returns <0 if strA is less than strB, 0 when they are equal, and >0 if strA is greater than strB.

like image 114
knittl Avatar answered Apr 21 '26 08:04

knittl


got the solution from here

Dynamic Linq 2 Sql using Expressions Trees raising exception "Binary Operator LessThan not defined for System.String and System.String"

switch ((operatorType)opType) {

 case operatorType.Greater: return Expression.GreaterThan(
                            Expression.Call(typeof(string),
                            "Compare", null, new[] { argLeft, argRight }),
                             Expression.Constant(0, typeof(int)));

} 
like image 24
sivaL Avatar answered Apr 21 '26 08:04

sivaL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!