How to merge result IQueryable<T> together?

Viewed 26906

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 Answers

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

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.

Related