我目前正在从事一个使用 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
- 检索所有行
- 将所有这些数据映射到一个大模型中(在内存中)
- 循环遍历所有模型,然后只返回适用的模型
由于这是我问题的症结所在,如果上面的流程会发生什么,我认为这是有道理的,但是显然比第 4 步更了解该框架的人一直在争论它以某种方式被纳入 SQL代,所以它不会撤回所有记录,但我不知道它是如何做到的,因为它需要预先所有数据来填充它,然后它会应用一个单独的 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"));
因此,鉴于上面的示例,最终的 Where 语句是否会被考虑到底层查询中,或者 viewModel 上的 where 会导致检索然后在内存中进行评估?
很抱歉这个问题很冗长,但是关于它的文档很少,这是一个非常具体的问题。