Gro*_*fit 3 c# linq database linq-to-sql
我目前不得不处理一个使用linq2sql作为其数据库访问框架的项目,现在有很多linq查询基本上执行以下操作:
var result = from <some_table>
join <some_other_table>
join <another_table>
select <some_other_domain_model> // This is a non linq2SQL poco
return result.Where(<Some_Predicate>);
Run Code Online (Sandbox Code Playgroud)
因此,例如假设您阅读了3个表,然后将内容整理为一个更高级别的模型,以便发送到视图.现在忽略域的混合,因为这并没有太多困扰我,它是最后的where子句.
现在我还没有使用过很多Linq2Sql,所以我说对了会发生什么是正确的:
由于这是我的问题的关键所在,如果上面的流程是会发生的,那么在我的脑海中是有道理的,但是那些显然比第4步更好地了解框架的人已经讨论过这个问题.生成因此它不会撤回所有记录,但我不知道它是如何做到这一点,因为它需要预先填写所有数据,然后它应用一个单独的where子句,所以我假设到第4点行已全部被读取并且已经在内存中.
我试图推动他们将where子句移动到linq中,以便在数据库级别过滤掉不需要的记录,但是我想知道是否有人可以建议我上面的假设是否正确?
==编辑==
添加了注释以引起更多注意这个事实,即不是linq2sql生成的对象,并且是一些随机的poco手动滚动到其他地方,只是为了缩小我的主要焦点在问题的上下文.因为问题很少"does it matter where I put the where clause"
而且更多"Does the where clause still get factored into the underlying query when it is applied to a non linq2sql object generated from a linq2sql query"
.
这是另一个更简洁的例子,我的意思是希望更多地指出我缺乏理解的地方:
/*
I am only going to put auto properties into the linq2sql entities,
although in the real world they would be a mix of private backing
fields with public properties doing the notiftying.
*/
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.some_table_1")]
public class SomeLinq2SqlTable1
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_1_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id {get;set;}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.some_table_2")]
public class SomeLinq2SqlTable2
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_2_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id {get;set;}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_2_name", AutoSync=AutoSync.OnInsert, DbType="Varchar NOT NULL", IsPrimaryKey=false)]
public string Name {get;set;}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.some_table_3")]
public class SomeLinq2SqlTable3
{
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_3_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id {get;set;}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="some_table_3_other", AutoSync=AutoSync.OnInsert, DbType="Varchar NOT NULL", IsPrimaryKey=false)]
public string Other {get;set;}
}
/*
This is some hand rolled Poco, has NOTHING to do with Linq2Sql, think of it as
a view model of sorts.
*/
public class SomeViewModel
{
public int Id {get;set;}
public string Name {get;set;}
public string Other {get;set;}
}
/*
Here is psudo query to join all tables, then populate the
viewmodel item from the query and finally do a where clause
on the viewmodel objects.
*/
var result = from // Linq2SqlTable1 as t1
join // Linq2SqlTable2.id on Linq2SqlTable1.id as t2
join // Linq2SqlTable3.id on Linq2SqlTable1.id as t3
select new ViewModel { Id = t1.Id, Name = t2.Name, Other = t3.Other }
return result.Where(viewModel => viewModel.Name.Contains("some-guff"));
Run Code Online (Sandbox Code Playgroud)
因此,在上面的示例中,最终的Where语句是否会被考虑到基础查询中,或者viewModel上的where会导致检索然后在内存中进行评估?
很抱歉这个问题的详细程度,但关于它的文档很少,这是一个非常具体的问题.
您不需要再推高该Where
子句.这是好的地方是,只要result
是IQueryable<T>
(一些T
).LINQ是可组合的.实际上,使用LINQ语法与使用扩展方法语法完全没有区别,并且两者都可以相同地工作.基本上,当您创建查询时,它只构建已请求的模型.什么执行,直到你开始迭代它(foreach
,ToList()
,等).所以最后添加一个额外Where
的东西很好:它将内置到组合查询中.
您可以通过监视SQL连接来非常简单地验证这一点; 你会看到它包含where
TSQL中的子句,以及SQL服务器上的过滤器.
这允许一些有趣的场景,例如灵活的搜索:
IQueryable<Customer> query = db.Customers;
if(name != null) query = query.Where(x => x.Name == name);
if(region != null) query = query.Where(x => x.Region == region);
...
if(dob != null) query = query.Where(x => x.DoB == dob);
var results = query.Take(50).ToList();
Run Code Online (Sandbox Code Playgroud)
就你的假设而言,它们是不正确的 - 它确实是:
请注意,只有在迭代查询时才会生成sql生成; 在那之前你可以整天保持作曲.在迭代之前,它不会触及SQL服务器.