Concat IQueryable collections in one db request

I am using entity framework.I need two collections concat. 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

      

Both options 1 and 2 will make queries 2 on the database because these methods need an IEnumerable parameter as a parameter. So the question is if I can combine two Iqueryble collections with one database call

+3


source to share


1 answer


There are Concat()

also Union()

extension methods for IQueryable<T>

in addition to IEnumerable<T>

. You can use them on the requested objects. Union()

maps the SQL operation UNION

, and Concat()

- to UNION ALL

.

As long as you don't convert objects IQueryable<T>

to IEnumerable<T>

(explicitly or implicitly), you can just use these methods and they will be evaluated in the database if possible.

Further reading:




Looking at the documentation, I masked the details that extension methods declared on Queryable

accept IQueryable<T>

as the first parameter and IEnumerable<T>

as the second. As D Stanley points out, these methods will check if the argument IQueryable<T>

is and if it tries to resolve the operation using the appropriate request mechanism.

+5


source







All Articles