问题描述
我遇到了麻烦LINQ翻译一些东西到我需要的查询。在T-SQL中,我们做了< =和三列是CHAR(6)列> =比较。 LINQ不会允许,因为我做这件事
我有下面的T-SQL查询。
SELECT *
FROM [ZIPMASTER] ZM
WHERE zm.CORP = 12
和'85546'zm.ZIPBEG和zm.ZIPEND间
以上是不是很LINQ freindly,由于没有用于不支持在
。因此,我已经简化为以下内容:
SELECT *
FROM [ZIPMASTER] ZM
WHERE zm.CORP = 12
与zm.ZIPBEG< ='85546'
与zm.ZIPEND> ='85546'
这是我用来创建以下LINQ查询:
VAR zipLinqQuery =
从ž在db.ZIPMASTERs
其中,z.CORP == 12
&功放;&安培; z.ZIPBEG< =85546
&功放;&安培; z.ZIPEND> =85546
特定的z;
名单< ZIPMASTER>拉链= zipLinqQuery.ToList< ZIPMASTER>();
C# - LINQ是不是喜欢这个查询太多。我想转换为整数,然后比较,但是,在某些情况下,压缩code可能包含一个字母。例如,下面的EX pression想要得到真正的T-SQL:
其中85546B之间85546A和85546D
我不知道为什么它的作品在T-SQL,但我的猜测是,它在数组中通过将其转换为数字ASCII值进行比较,一个个字符分别。
总之,任何帮助,您都可以提供大大AP preciated。先谢谢了。
CJAM
解决方案(发表乔恩飞碟双向):
看来,string.CompareTo()事实上确实生成所需的T-SQL。下面的例子:
VAR zipLinqQuery =
从ž在db.ZIPMASTERs
其中,z.CORP == listItem.CORP
&功放;&安培; z.ZIPBEG.CompareTo(listItem.ZIP code)< = 0
&功放;&安培; z.ZIPEND.CompareTo(listItem.ZIP code)> = 0
特定的z;
生成以下的T-SQL:
DECLARE @ P0 INT,@ P1 CHAR(6),@ p2的CHAR(6)
SET @ P0 = 12
SET @ P1 ='85546'
SET @ P2 ='85546'
选择[T0]。[CORP],[T0]。[ZIPEND],[T0]。[ZIPBEG],[T0] [市],[T0]。[状态],[T0]。[CYCLE]
FROM [DBO]。[ZIPMASTER] AS [T0]
WHERE([T0] [CORP] = @ P0)与([T0] [ZIPBEG]其中; = @ P1)和([T0] [ZIPEND]≥。= @ p2的)
尝试:
VAR zipLinqQuery =
从ž在db.ZIPMASTERs
其中,z.CORP == 12
&功放;&安培; z.ZIPBEG.CompareTo(85546)< = 0
&功放;&安培; z.ZIPEND.CompareTo(85546)> = 0
特定的z;
我不的知道的是String.CompareTo工作在LINQ到SQL,但它的尝试的第一件事。
(通常情况下,你应该使用一个StringComparer指定正确类型的比较,但我怀疑在这种情况下的CompareTo是更好的选择。)
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
I have the following T-SQL query..
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:
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:
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:
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.
Anyway, any help you all can provide is greatly appreciated. Thanks in advance.
CJAM
Solution (posted by Jon Skeet):
It appears that string.CompareTo() does in fact generate the needed T-SQL. Examples below:
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:
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)
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 know that String.CompareTo works in LINQ to SQL, but it's the first thing to try.
(Normally you should use a StringComparer to specify the right type of comparison, but I suspect in this case CompareTo is the better option.)
这篇关于这样做在LINQ字符串比较的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!