Search code examples
c#linqentity-frameworkiqueryable

Concat Two IQueryables with Anonymous Types?


I've been wrestling with this a little while and it's starting to look like it may not be possible.

I want to Concat() two IQueryables and then execute the result as a single query. I tried something like this:

var query = from x in ...
select new
{
    A = ...
    B = ...
    C = ...
};

var query2 = from y in ...
select new
{
    A = ...
    B = ...
    C = ...
};

var query3 = query.Concat(query2);

However, the last line gives me the following error:

'System.Linq.IQueryable' does not contain a definition for 'Concat' and the best extension method overload 'System.Linq.ParallelEnumerable.Concat(System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)' has some invalid arguments

It appears it's expecting an IEnumerable for the argument. Is there any way around this?

It looks like I could resolve both queries to IEnumerables and then Concat() them. But it would be more efficient to create a single query, and it seems like that should be possible.


Solution

  • You can't.

    As you said previously in the comments, it seems that the two queries return different objects:

    Query 1 (as per comment):

    f__AnonymousTypee<Leo.Domain.FileItem,Leo.Domain.Employ‌​ee,int,string,string>
    

    Query2 is

    f__AnonymousTypee<Leo.Domain.FileItem,L‌​eo.Domain.Employee,int?,string,string>
    

    This is why Concat is giving you an error message complaining about invalid arguments.