Lambda expression Compare operator “>”,“>=” issue

2019-10-21 06:10发布

问题:

when I execute these operators( > and >=) lambda expression in entity framework. both are getting same results.

  1. db.Companies.where(Company => (Compare(Convert(Company.Name), "y") > 0))
  2. db.Companies.where(Company => (Compare(Convert(Company.Name), "y") >= 0))

Is that issue with Lambda expression compare operator? i changed to

  1. db.Companies.where(Company => (Compare(Convert(Company.Name), "y") > 1)) - No results. Its not correct

  2. db.Companies.where(Company => (Compare(Convert(Company.Name), "y") >= 1)) - 64 results

source code

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

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

回答1:

If you're gettnig the same results, means that there is no Company.Name equal to "Y"



回答2:

Why would you expect the answer to be different.

  • What does Compare do?
  • What does Convert do?
  • What does Compare(Convert(Company.Name), "y") evaluate to?

If it evaluates to 1 or greater then both answers would be correct.



回答3:

If you have the same number of results for both queries then it means that (Compare(Convert(Company.Name), "y") is never equal to zero. If it is anything greater than zero both queries will return it; if it's anything less than zero then neither query will return it.



回答4:

Thanks for all responses The Lambda expression query is correct only.

db.Companies.where(Company => (Compare(Convert(Company.Name), "Test") > 0))
db.Companies.where(Company => (Compare(Convert(Company.Name), "Test") >= 0))

I changed the "right arg" value to "Test". I have one record 'Name' with Test. executed the following query.

db.Companies.where(Company => (Compare(Convert(Company.Name), "Test") > 0)) 

and shown the results 105(here Name with 'Test' is not shown. - Correct

Then I executed this

db.Companies.where(Company => (Compare(Convert(Company.Name), "Test") >= 0)) 

and shown the results 106(here Name with 'Test' is shown) - Correct



标签: c# lambda