Moh*_*ori 6 c# entity-framework-6
我在C#中使用Entity Framework,我的代码是
var result = ef.services.Where(entry => entry.tarikhservice >= textBoxX1.Text
&& entry.tarikhservice <= textBoxX2.Text).ToList();
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
运算符'> ='不能应用于'string'和'string'类型的操作数
如何比较两个字符串并修复错误?
ya2*_*a23 18
比较数字时,例如1和2,很明显哪一个更大.但是,当您比较字符串时,哪一个被认为更大:"2"或"11"?"foo"或"f"?答:这取决于背景.例如,如果按字典顺序对它们进行排序,则会得到"2"和"f".如果你想要自然排序,你会在"11"之前得到"2".
我假设因为这个原因,相对运算符(>,> =,<,<=)不会因字符串而过载(恕我直言,这是一个很好的决定).
您可以选择编写自定义逻辑来比较字符串,或使用框架提供的词典比较.代码将是(如果我得到正确的数字):
var result = ef.services.Where(entry =>
string.Compare(entry.tarikhservice, textBoxX1.Text) >= 0
&& string.Compare(entry.tarikhservice, textBoxX2.Text) <= 0
.ToList()
Run Code Online (Sandbox Code Playgroud)
要使代码工作而不管文化(你应该!),提供StringComparison作为string.compare的最后一个参数:
string.Compare(entry.tarikhservice, textBoxX1.Text, StringComparison.InvariantCulture)
Run Code Online (Sandbox Code Playgroud)