12

I'm using Entity Framework in C# and my code is

var result = ef.services.Where(entry => entry.tarikhservice >= textBoxX1.Text
                                     && entry.tarikhservice <= textBoxX2.Text).ToList();

which gives me this error:

Operator '>=' cannot be applied to operands of type 'string' and 'string'

How to compare two string and fix the error?

Mohammadreza Noori
  • 181
  • 1
  • 2
  • 5
  • 3
    You should use `CompareTo`. that allows you to specify culture/ –  Feb 18 '15 at 11:35
  • 3
    Could you please explain how semantically you decide if one string is greater than another? – Yuval Itzchakov Feb 18 '15 at 11:36
  • 1
    @YuvalItzchakov - Count the number of votes its received :) – Sayse Feb 18 '15 at 11:37
  • "How to compare to string" depends on what you're comparing and how you want them to be compared. Are they actually numbers? Sequential string labels? Be more clear. – J. Steen Feb 18 '15 at 11:40
  • 1
    possible duplicate of [Entity framework strings using greater than operator](http://stackoverflow.com/questions/9380985/entity-framework-strings-using-greater-than-operator) – ASh Feb 18 '15 at 11:40
  • Assuming that you are trying to compare two dates read from a textbox, you should either use DatePicker instead of TextBox or use converted values as result = ef.services.Where(entry => DateTime.Parse(entry.tarikhservice) >= DateTime.Parse(textBoxX1.Text) && DateTime.Parse(entry.tarikhservice) <= DateTime.Parse(textBoxX2.Text)).ToList(); Please bear in mind that this coe is not safe and might raise Exception if the values are not set or properly set. – user3021830 Feb 18 '15 at 11:41

3 Answers3

25

When you compare numbers, say 1 and 2, it is clear which one is greater. However, when you compare strings, which one is considered greater: "2" or "11"? "foo" or "f"? Answer: it depends on context. For example if you sort them lexicographically, you get "2" and "f". If you want the natural sort, you would get "2" before "11".

I presume for that reason, relative operators (>, >=, <, <=) are not overloaded for string (which IMHO is a good decision).

Your option is to either write your custom logic to compare strings, or use a framework-provided lexicographical comparison. The code would be (if I got the numbers right):

var result = ef.services.Where(entry => 
        string.Compare(entry.tarikhservice, textBoxX1.Text) >= 0
     && string.Compare(entry.tarikhservice, textBoxX2.Text) <= 0
   .ToList()

To make code work regardless of culture (you should!), provide a StringComparison as last parameter to string.compare:

string.Compare(entry.tarikhservice, textBoxX1.Text, StringComparison.InvariantCulture)
ya23
  • 14,226
  • 9
  • 46
  • 43
2

I'm not sure I agree with the rationale given in the current answers for not implementing the comparison operators in String. In fact, I submit that implementing them using String.Compare would be just as consistent as the current equality operator implementation using String.Equals.

I have a different theory. If you examine the Best Practices for Using Strings in .NET you will note the following recommendation:

The String class lets you test for equality by calling either the static or instance Equals method overloads, or by using the static equality operator. The overloads and operator use ordinal comparison by default. However, we still recommend that you call an overload that explicitly specifies the StringComparison type even if you want to perform an ordinal comparison

Note that they couldn't enforce this recommendation even if they wanted to - even if they didn't override the == operator in String, you could still do str1 == str2. Heck, you could compare an integer to a string using the equality operator if you really wanted to (obj i = 1). This is because == translates into reference equality when not overridden specifically for the operand types.

The BCL designers realized this and decided they would be better off implementing a comparison operator that works as expected most of the time, rather than delegate the comparison to reference equality, which would be a really bad idea considering string interning and Unicode equivalence.

Now, back to comparison operators. The same pitfalls with string equality apply, but the BCL designers have an easier time here. By simply not implementing these operators, the compiler does their job for them and, as we can see in this question, prevents devs from even trying to do things like str1 >= str2.

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
1

It is quite reasonable that you take. Two strings can be equal or not equal. Nothing less nothing more. The greater and the less operator makes sense, when you compare numbers, or if you compare objects for which you have overloaded this operators.

As it is stated here,

A string is not a number, so you're comparing lexicographically. String.CompareTo is used for ordering.

Community
  • 1
  • 1
Christos
  • 53,228
  • 8
  • 76
  • 108
  • 4
    And SQL generally allows using `<` and `>` in string comparison (meaning a different thing that the OP probably expects, granted) – Jcl Feb 18 '15 at 11:37
  • PS: I haven't downvoted since I still feel the answer is "somehow" correct. As it turns out, @ya23's answer is just more spot-on (saying basically the same) – Jcl Feb 18 '15 at 11:45