我目前正在开发一个使用 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>);
例如,假设您读取了 3 个表,然后将内容整理到一个更大的更高级别模型中,以发送到视图。现在忽略域的混合,因为这并没有太困扰我,它的最后一个 where 子句确实如此。
现在我之前没有太多使用过 Linq2Sql,所以我正确地说将要发生的事情是:
- 基于 from、join、join、select linq 生成 SQL
- 检索所有行
- 将所有这些数据映射到一个大模型(在内存中)
- 循环遍历所有模型,然后仅返回适用的模型
因为这是我问题的关键,所以如果上面的流程是会发生的情况,在我看来这是有道理的,但显然比第四步以某种方式纳入 SQL 中的第四步更了解该框架的人对此进行了辩论生成,因此它不会拉回所有记录,但我不知道它是如何做到这一点的,因为它需要预先填充所有数据,然后应用一个单独的 where 子句,所以我假设第四点行已全部读取并已在内存中。
我试图推动他们将 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"));
因此,考虑到上面的示例,最终的Where语句是否会被考虑到底层查询中,或者viewModel上的where是否会导致检索,然后在内存中求值?
很抱歉这个问题很冗长,但是关于它的文档很少,这是一个非常具体的问题。