简体   繁体   English

Linq为什么忽略我的where子句?

[英]Why does Linq ignore my where clause?

I see several StackOverflow questions on this already but not of them seem to match my scenario. 我已经看到了几个StackOverflow问题,但它们似乎与我的情况不符。 I promise I looked. 我保证我看了。

I have some queries against my database that I'm using linq to do and I can't figure out why the incorrect SQL is being generated. 我对我的数据库有一些查询,我正在使用linq来做,我无法弄清楚为什么生成错误的SQL。 This is happening in several places in my code. 这在我的代码中的几个地方发生。 I'm hoping we're just falling into some well known gotcha but I can't wrap my head around why Linq seemingly decides my where clause is dumb and shouldn't add it to the generated SQL query. 我希望我们只是陷入一些众所周知的陷阱,但我无法理解为什么Linq似乎决定我的where子句是愚蠢的,不应该将它添加到生成的SQL查询中。

Why is this? 为什么是这样?

Example: 例:

var testing = (from i in context.TableName1 where i.Param1 == object1.GuidParam select i).ToList();

The above query returns the following SQL 以上查询返回以下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]}

However the following query: 但是以下查询:

var testing = (from i in context.TableName1 where i.Param1 == object1.GuidParam select i);
var testingToList = testing.ToList();

Generates the following correct SQL 生成以下正确的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'}

I prefer the lambda notation, and I don't see why this wouldn't work... 我更喜欢lambda表示法,我不明白为什么这不起作用......

var testing = context.TableName1.Where(i => i.Param1 == object1.GuidParam).ToList();

Cleaner, concise and it should work. 更清洁,简洁,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM