C# 在 LINQ 中进行字符串比较的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/578231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issues Doing a String Comparison in LINQ
提问by regex
I'm having trouble getting LINQ to translate something into the query I need. In T-SQL, we do a <= and >= comparison on three columns that are CHAR(6) columns. LINQ will not allow me to do this since
我无法让 LINQ 将某些内容转换为我需要的查询。在 T-SQL 中,我们对三个 CHAR(6) 列进行 <= 和 >= 比较。LINQ 不允许我这样做,因为
Operator '<=' cannot be applied to operands of type 'string' to 'string'.
运算符“<=”不能应用于“字符串”到“字符串”类型的操作数。
I have the following T-SQL query..
我有以下 T-SQL 查询..
SELECT *
FROM [ZIPMASTER] zm
WHERE zm.CORP = 12
AND '85546 ' BETWEEN zm.ZIPBEG AND zm.ZIPEND
The above is not very LINQ freindly, since there is no support for BETWEEN
. Thus, I have simplified to the following:
以上不是非常友好的 LINQ,因为不支持BETWEEN
. 因此,我已简化为以下内容:
SELECT *
FROM [ZIPMASTER] zm
WHERE zm.CORP = 12
AND zm.ZIPBEG <= '85546 '
AND zm.ZIPEND >= '85546 '
Which I have used to create the following LINQ query:
我用来创建以下 LINQ 查询:
var zipLinqQuery =
from z in db.ZIPMASTERs
where z.CORP == 12
&& z.ZIPBEG <= "85546 "
&& z.ZIPEND >= "85546 "
select z;
List<ZIPMASTER> zips = zipLinqQuery.ToList<ZIPMASTER>();
C# - LINQ is not liking this query too much. I tried converting to ints and then comparing, however, in some cases the zip code might contain a letter. For example, the following expression would evaluate to true in T-SQL:
C# - LINQ 不太喜欢这个查询。我尝试转换为整数然后进行比较,但是,在某些情况下,邮政编码可能包含一个字母。例如,以下表达式在 T-SQL 中的计算结果为 true:
WHERE '85546B' BETWEEN '85546A' AND '85546D'
I don't know exactly why it works in T-SQL, but my guess is that it compares each character in the array individually by converting it to a numerical ASCII value.
我不知道它为什么在 T-SQL 中工作,但我的猜测是它通过将数组中的每个字符转换为数字 ASCII 值来单独比较数组中的每个字符。
Anyway, any help you all can provide is greatly appreciated. Thanks in advance.
无论如何,非常感谢你们能提供的任何帮助。提前致谢。
CJAM
CJAM
Solution (posted by Jon Skeet):
解决方案(由 Jon Skeet 发布):
It appears that string.CompareTo() does in fact generate the needed T-SQL. Examples below:
看来 string.CompareTo() 实际上确实生成了所需的 T-SQL。下面的例子:
var zipLinqQuery =
from z in db.ZIPMASTERs
where z.CORP == listItem.CORP
&& z.ZIPBEG.CompareTo(listItem.ZIPCODE) <= 0
&& z.ZIPEND.CompareTo(listItem.ZIPCODE) >= 0
select z;
Generates the following T-SQL:
生成以下 T-SQL:
DECLARE @p0 INT, @p1 CHAR(6), @p2 CHAR(6)
SET @p0 = 12
SET @p1 = '85546 '
SET @p2 = '85546 '
SELECT [t0].[CORP], [t0].[ZIPEND], [t0].[ZIPBEG], [t0].[CITY], [t0].[STATE], [t0].[CYCLE]
FROM [dbo].[ZIPMASTER] AS [t0]
WHERE ([t0].[CORP] = @p0) AND ([t0].[ZIPBEG] <= @p1) AND ([t0].[ZIPEND] >= @p2)
采纳答案by Jon Skeet
Try:
尝试:
var zipLinqQuery =
from z in db.ZIPMASTERs
where z.CORP == 12
&& z.ZIPBEG.CompareTo("85546 ") <= 0
&& z.ZIPEND.CompareTo("85546 ") >= 0
select z;
I don't knowthat String.CompareTo works in LINQ to SQL, but it's the first thing to try.
我不知道String.CompareTo 在 LINQ to SQL 中有效,但这是首先要尝试的。
(Normally you should use a StringComparer to specify the right type of comparison, but I suspect in this case CompareTo is the better option.)
(通常您应该使用 StringComparer 来指定正确的比较类型,但我怀疑在这种情况下 CompareTo 是更好的选择。)