[已解决 - 请参阅更新]
我有这个L2S查询(看起来很可怕,但是出错的地方很简单):
var historicDataPointValues = from dpv in this._dataContext.DataPointValues
where (dpv.categoryId == historicCategoryId)
&& (dpv.retailerId == historicRetailerId)
&& (dpv.questionId == question.PreviousQuestionId)
&& historicResponseIds.Contains(dpv.responseIndex)
&& ((dpv.dataPointIndex == firstDataPointIndex) || (dpv.dataPointIndex == secondDataPointIndex))
select new KeyValuePair<string, double>(MakeDataPointValueKey(dpv), dpv.value);
...没有产生任何结果,所以我查看了L2S发出的SQL。
当我执行它时,firstDataPointIndex的值为1,secondDataPointIndex为null。然而,L2S产生的查询(如调试器中所示)是:
SELECT [t0].[categoryId], [t0].[retailerId], [t0].[questionId], [t0].[responseIndex], [t0].[dataPointIndex], [t0].[value]
FROM [dbo].[DataPointValue] AS [t0]
WHERE ([t0].[categoryId] = 1)
AND ([t0].[retailerId] = 1)
AND (([t0].[questionId]) = 8)
AND ([t0].[responseIndex] IN (1, 3, 4, 5, 6, 7, 8, 10, 11, 12))
AND (([t0].[dataPointIndex] = 13) OR ((CONVERT(Int,[t0].[dataPointIndex])) = 14))
注意值13&amp; 14其中firstDataPointIndex和secondDataPointIndex应该是(它们应该是1和NULL;如果我输入正确的值,那么我得到我期望的结果)。
WTF吗
UPDATE :原来L2S调试器出错了。它没有显示与实际发送到服务器的SQL相同的SQL,即:
exec sp_executesql N'SELECT [t0].[categoryId], [t0].[retailerId], [t0].[questionId], [t0].[responseIndex], [t0].[dataPointIndex], [t0].[value]
FROM [dbo].[DataPointValue] AS [t0]
WHERE ([t0].[categoryId] = @p0) AND ([t0].[retailerId] = @p1) AND (([t0].[questionId]) = @p2) AND ([t0].[responseIndex] IN (@p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12)) AND (([t0].[dataPointIndex] = @p13) OR ((CONVERT(Int,[t0].[dataPointIndex])) = @p14))',N'@p0 int,@p1 int,@p2 int,@p3 int,@p4 int,@p5 int,@p6 int,@p7 int,@p8 int,@p9 int,@p10 int,@p11 int,@p12 int,@p13 int,@p14 int',@p0=1,@p1=1,@p2=8,@p3=1,@p4=3,@p5=4,@p6=5,@p7=6,@p8=7,@p9=8,@p10=9,@p11=10,@p12=11,@p13=1,@p14=NULL
请注意,查询现在包含@ p13和@ p14,其中之前它有标量值。