问题
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?
回答1:
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)
回答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
.
回答3:
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.
来源:https://stackoverflow.com/questions/28582598/operator-cannot-be-applied-to-operands-of-type-string-and-string