我已经看到了几个 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'}