在1分贝要求的毗连IQueryable的集合 [英] Concat IQueryable collections in one db request
问题描述
我用实体framework.I需要CONCAT 2 collections.For例如:
I use entity framework.I need concat two collections.For example:
IQueryable<SomeType> collection1 = context.Entiies.Where(predicate);
IQueryable<SomeType> collection2 = context.Entiies.Where(otherPredicate);
var result = collection1.concat(collection2).toList(); //1
var result = collection1.union(collection2).toList; //2
1和2都变会做的 2 在数据库请求,因为这些方法需要的IEnumerable作为parameter.So,问题是,我可以Concat的2 Iqueryble集合一个数据库调用
Both 1 and 2 variant will do 2 requests in database,because these methods need IEnumerable as parameter.So,the question is can I concat two Iqueryble collections with one database call
推荐答案
有 CONCAT()
和的 联盟()
扩展方法为的IQueryable< T>
除了的IEnumerable< T>
。您可以在可查询的对象使用。 联盟()
映射到SQL UNION
运行和 CONCAT()
映射到 UNION ALL
。
There are Concat()
and Union()
extension methods for IQueryable<T>
in addition to IEnumerable<T>
. You can use them on queryable objects. Union()
maps to the SQL UNION
operation and Concat()
maps to UNION ALL
.
只要你不转换 IQueryable的< T>
反对的IEnumerable< T>
(显式或隐式),那么你可以使用这些方法,他们将在评估。数据库如果可能的话
As long as you don't convert the IQueryable<T>
objects to IEnumerable<T>
(either explicitly or implicitly) then you can just use these methods and they will be evaluated in the database if possible.
延伸阅读:
- 的 MSDN文档的
可查询
类.NET 4.5 。此记录的所有可能潜在数据库中,而不是在CLR被评估的扩展方法。
- MSDN documentation for the
Queryable
class in .NET 4.5. This documents all of the extension methods that can potentially be evaluated in the database instead of in the CLR.
在通过文档一眼我掩盖了,关于可查询
接受声明的扩展方法的细节的IQueryable< T>
作为第一个参数,但的IEnumerable< T>
作为第二个。为D斯坦利指出,这些方法将测试该参数是的IQueryable< T>
如果这样将尝试使用相关的查询引擎,以解决操作
In glancing through the documentation I glossed over the detail that the extension methods declared on Queryable
accept IQueryable<T>
as the first parameter, but IEnumerable<T>
as the second. As D Stanley points out, these methods will test if the argument is an IQueryable<T>
and if so will attempt to resolve the operation using the relevant query engine.
这篇关于在1分贝要求的毗连IQueryable的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!