【问题标题】:Linq select returned where clause fieldLinq 选择返回 where 子句字段
【发布时间】:2014-09-18 18:03:38
【问题描述】:
var AbbA = (from t1 in mdc.table1 
join t2 in mdc.table2 on t1.id equals t2.id
select new { t1, t2 }).AsQueryable();

if(a=1)
   AbbA=AbbA.Where(q=>q.t1.cid==x);
else
   AbbA=AbbA.Where(q=>q.t1.cid==y);

var Global=Abba.Select(q=> new{NewName1 = t1.field1, NewName2=t2.field2}).ToList();

我只想列出 2 个字段。但返回了 2 个字段和 where 子句字段。

配置文件视图

exec sp_executesql N'SELECT 
    [Extent1].[cid] AS [cid], >>> This is where clause filed
    [Extent1].[field1] AS [NewName1], 
    [Extent2].[field2] AS [NewName2]
    FROM  [dbo].[table1] AS [Extent1]
    INNER JOIN [dbo].[table2] AS [Extent2] ON [Extent1].[id] = [Extent2].[id]
    WHERE [Extent1].[cid] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=10000

【问题讨论】:

  • cid 字段具有聚集 (IX_INDEX) 索引。是因为这个原因吗?
  • 这行这里会添加一个where子句AbbA=AbbA.Where(q=>q.t1.cid==x);

标签: sql linq dynamic


【解决方案1】:

我想我明白你的问题了...为什么生成的 sql 包含 where 子句?

这里的这一行将添加一个 where 子句,因为它仍在形成可查询的表达式树

AbbA=AbbA.Where(q=>q.t1.cid==x); 

如果您想从生成的 sql 中删除 where 子句,然后在查询末尾添加 ToList(),然后您将使用 linq to objects 操作结果,然后您将在内存中操作它们之后的实际对象已经从sql中检索到了。

var AbbA = (from t1 in mdc.table1 
join t2 in mdc.table2 on t1.id equals t2.id
select new { t1, t2 }).ToList();

【讨论】:

  • 你好,@Twisted Inferno,>>为什么生成的 sql 包含 where 子句?是的! cid 字段是索引字段。我尝试了另一个没有返回的 where 子句。
  • 对不起,我不太明白你在说什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
相关资源
最近更新 更多