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

2019-10-21 06:03发布

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

标签: c# lambda
4条回答
虎瘦雄心在
2楼-- · 2019-10-21 06:21

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

查看更多
forever°为你锁心
3楼-- · 2019-10-21 06:26

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楼-- · 2019-10-21 06:26

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

查看更多
萌系小妹纸
5楼-- · 2019-10-21 06:28

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.

查看更多
登录 后发表回答