Entity framework strings using greater than operator

11,378

Try this :

from r in results
where (r.ExemptionCode.CompareTo("900") > 0  || r.ExemptionCode == "701" || r.ExemptionCode == "702" ||     r.ExemptionCode == "721" || r.ExemptionCode == "724")
select r
Share:
11,378

Related videos on Youtube

Doug Chamberlain
Author by

Doug Chamberlain

Updated on June 04, 2022

Comments

  • Doug Chamberlain
    Doug Chamberlain about 2 years

    How do I make this query work like it does in sql? In sql I can use < and > operators on strings.

    I've been googling this for about 20 minutes and have found no solution yet.

    I cannot convert r.ExemptionCode to an integer as it may have values like '91A,9AA,ZZZ,Z01'

    from r in results
    where (r.ExemptionCode > "900"  || r.ExemptionCode == "701" || r.ExemptionCode == "702" || r.ExemptionCode == "721" || r.ExemptionCode == "724")
    select r
    
  • Doug Chamberlain
    Doug Chamberlain over 12 years
    Copy and pasted straight from my brain. Good Job. I figured this out after extensive use of the immediate window! XD
  • jim tollan
    jim tollan over 10 years
    altho it didn't solve my issue based on the answer, you pointed me in the correct direction, which was in my case String.Compare(a.version, b.version, System.StringComparison.Ordinal) > 0 - thanks +1
  • Daniel Hillebrand
    Daniel Hillebrand over 4 years
    Works with EF Core 3.1 also. If someone wonders. SomeTables.Where(l => l.SomeField.CompareTo("A 102") >= 0 && l.SomeField.CompareTo("A 104") <= 0) generated SELECT [s].[someField] FROM [SomeTable] AS [s] WHERE (CASE WHEN [s].[someField] = N'A 102' THEN 0 WHEN [s].[someField] > N'A 102' THEN 1 WHEN [s].[someField] < N'A 102' THEN -1 END >= 0) AND (CASE WHEN [s].[someField] = N'A 104' THEN 0 WHEN [s].[someField] > N'A 104' THEN 1 WHEN [s].[someField] < N'A 104' THEN -1 END <= 0) in my case.
  • Eric Patrick
    Eric Patrick over 3 years
    While this works, it appears less than optimal in the SQL sense. Generating a WHERE CASE statement pulls every possible record from the DB, to be filtered in the application tier. Any suggestions on a LINQ expression that generates something like WHERE LastName BETWEEN 'Pat' AND 'Pet'
  • Eric Patrick
    Eric Patrick over 3 years
    A bit more research on my side, and this answer appears to be an excellent approach to this problem: stackoverflow.com/a/59095579/1088293