我已经看到了几个StackOverflow问题,但它们似乎与我的情景不符。我保证我看了。
我对我的数据库有一些查询,我正在使用linq来做,我无法弄清楚为什么生成了错误的SQL。这在我的代码中的几个地方发生。我希望我们只是陷入一些众所周知的陷阱,但我无法理解为什么Linq似乎决定我的where子句是愚蠢的,不应该将它添加到生成的SQL查询中。
为什么会这样?
示例:
var testing = (from i in context.TableName1 where i.Param1 == object1.GuidParam select i).ToList();
以上查询返回以下SQL
{SELECT
[Extent1].[RecordId] AS [RecordId],
[Extent1].[AnotherId] AS [AnotherId],
[Extent1].[YetAnotherId] AS [YetAnotherId],
[Extent1].[WeLikeIds] AS [WeLikeIds],
[Extent1].[WeReallyLikeIds] AS [WeReallyLikeIds]
FROM [dbo].[SomeTable] AS [Extent1]}
但是以下查询:
var testing = (from i in context.TableName1 where i.Param1 == object1.GuidParam select i);
var testingToList = testing.ToList();
生成以下正确的SQL
{SELECT
[Extent1].[RecordId] AS [RecordId],
[Extent1].[AnotherId] AS [AnotherId],
[Extent1].[YetAnotherId] AS [YetAnotherId],
[Extent1].[WeLikeIds] AS [WeLikeIds],
[Extent1].[WeReallyLikeIds] AS [WeReallyLikeIds]
FROM [dbo].[SomeTable] AS [Extent1]
WHERE [Extent1].[RecordId] = '78e49f5c-0ff8-e311-93f4-00155d514a6d'}
答案 0 :(得分:-2)
我更喜欢lambda符号,我不明白为什么这不会起作用......
var testing = context.TableName1.Where(i => i.Param1 == object1.GuidParam).ToList();
更清洁,简洁,它应该有用。