3

我目前正在从事一个使用 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,所以我可以说会发生什么是正确的:

  1. 根据 from、join、join、select linq 生成 SQL
  2. 检索所有行
  3. 将所有这些数据映射到一个大模型中(在内存中)
  4. 循环遍历所有模型,然后只返回适用的模型

由于这是我问题的症结所在,如果上面的流程会发生什么,我认为这是有道理的,但是显然比第 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 会导致检索然后在内存中进行评估?

很抱歉这个问题很冗长,但是关于它的文档很少,这是一个非常具体的问题。

4

3 回答 3

5

您无需将Where条款推高。只要resultIQueryable<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();

就您的假设而言,它们是不正确的-实际上是:

  1. 构建可组合查询,(单独)组合,连接,连接,选择
  2. 进一步编写查询,添加一个 where (与上述组合没有不同)
  3. 稍后,迭代查询
    1. 从完全组合的查询生成 sql
    2. 检索行
    3. 映射到模型
    4. 产生结果

请注意,只有在迭代查询时才会生成 sql;在那之前,您可以整天继续创作它。它在迭代之前不会触及 SQL 服务器。

于 2012-11-29T10:13:39.813 回答
0

我对 LINQtoSQL 最佳实践做了一些研究,因为我一直在我的项目中使用这项技术。看看我的博文。也许它可以帮助你。

http://msguy.net/post/2012/03/20/LINQ-to-SQL-Practices-and-approaches.aspx

于 2012-11-29T13:45:45.550 回答
0

提供者知道自定义模型中的填充属性如何映射(由于查询中的 select 子句)到数据库表上的实际列。因此,当您过滤自定义模型的属性时,它知道需要过滤表中的哪一列。将您选择的模型(无论是设计人员定义的具有表的所有列的实体,还是在某处定义的自定义模型,或仅包含您需要的数据的匿名类型)想像为只是 FROM 子句之前的选定列SQL 查询。选择匿名模型可以很容易地识别模型中的字段对应于 SQL 中的 SELECT 列表。

最重要的是:永远记住这var result = from ...只是一个查询......直到它被迭代result.ToArray()。尝试调用您的变量query而不是result,当您再次查看时,世界可能会获得新的新颜色。

于 2013-05-30T13:47:06.090 回答