Skip to content
Advertisement

What would be C# equivalent for SQL “>” operator in where clause for char column

working on an c# application i came across something that I don’t quite understand.

I have SQL column KONTO char(15) and it contains numeric data. When I run the following query…

I get this result…

enter image description here

and thats just fine.

My C# code providing the same result is:

I came across problem when I tried to select where greater than “01”

SQL did the job and I got

I tried doing something similar in C# but it gives me syntax error.

Because LINQ can’t use “>” on two strings. I tried using SqlCommand and Parameters to get the data and it works, but I am using EF and it seems its causing me problem.

Is there a way to do this using LINQ and EF? How does SQL do this “magic”?

Advertisement

Answer

Sql is comparing the byte value the coallition of the string. In c# you compare similar to that using the method string.Compare(s1,s2) it returns

something like this:

or if you need the numeric order of the strings you can cast it to an int :

keep in mind that :


Edit: changed byte value to coallition as @Gordon Linoff sugested

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement