我目前必须处理一个使用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,所以我说得对吗?
因为这是我问题的症结所在,如果上面的流是会发生的话,这在我的脑海中是有意义的,但是那些显然比第四步更了解这个框架的人对它进行了辩论,所以它不会在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会导致检索,然后在内存中进行计算?
很抱歉,这个问题很冗长,但关于这个问题的文件很少,这是一个相当具体的问题。
发布于 2012-11-29 10:13:39
您不需要将Where子句推得更高。只要result是IQueryable<T> (对某些T来说),就可以在那里使用。LINQ是可合成的。实际上,使用LINQ语法和使用扩展方法语法是完全没有区别的,而且两者的工作原理都是一样的。基本上,在创建查询时,只有才能构建所请求的查询的模型。在开始迭代之前,不会执行任何操作(foreach、ToList()等)。因此,在最后添加一个额外的Where是可以的:这将被内置到复合查询中。
您可以非常简单地通过监视SQL连接来验证这一点;您将看到它在TSQL中包含了where子句,并在server上进行了筛选。
这允许一些有趣的场景,例如灵活的搜索:
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();就你的假设而言,它们是不正确的--它确实是:
请注意,只有在查询被迭代时才会发生sql生成;在此之前,您可以整天编写它。它不访问SQL服务器,直到它被迭代。
发布于 2012-11-29 13:45:45
我做了一些关于LINQtoSQL最佳实践的研究,因为我总是在我的项目中使用这种技术。看看我的博客吧。也许它能帮到你。
http://msguy.net/post/2012/03/20/LINQ-to-SQL-Practices-and-approaches.aspx
发布于 2013-05-30 13:47:06
提供程序知道如何将自定义模型中填充的属性(因为查询中的select子句)映射到数据库表中的实际列。因此,当您对自定义模型的属性进行筛选时,它知道表中需要筛选的列。将所选模型(它是一个带有表中所有列的设计器定义的实体,或者某个地方定义的自定义模型,或者是带有所需数据的匿名类型)想象为SQL查询中FROM子句之前的选定列。选择匿名模型可以很容易地识别模型中的字段与SQL中的选择列表相对应。
最重要的是:永远记住var result = from ...只是一个查询..。直到它被迭代result.ToArray()。尝试调用您的变量query而不是result,,当您再次查看时,世界可能会有新的颜色。
https://stackoverflow.com/questions/13623344
复制相似问题