tags:

views:

421

answers:

3

If I get two result IQueryable from different linq Query and I want to merge them together and return one as result, how to to this? For example, if:

var q1 = (IQueryable<Person>).....;
var q2 = (IQueryable<Person>).....;

how to merge q1 and q2 together and get result like

var q = (IQueryable<Person>)q1.Union(q2);
+4  A: 

You have it, q1.Union(q2). The Union is in the System.Linq namespace with Queryable.

Yuriy Faktorovich
+2  A: 

You can try the Concat Method

Something like this

int[] i1 = new int[] { 1, 2, 3 };
int[] i2 = new int[] { 3, 4 };
//returns 5 values
var i3 = i1.AsQueryable().Concat(i2.AsQueryable());
//returns 4 values
var i4 = i1.AsQueryable().Union(i2.AsQueryable());

Union will only give you the DISTINCT values, Concat will give you the UNION ALL.

astander
The Union will use the default comparer, if he didn't specify one, each Person would likely have a different object reference.
Yuriy Faktorovich
A: 
(q1.Union(q2)).AsQuerable()
amarsuperstar
The Union method returns a Queryable.
Yuriy Faktorovich