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 IQueryable
s 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 IEnumerable
s and then Concat()
them. But it would be more efficient to create a single query, and it seems like that should be possible.
The IDE determined query and query2 are of different types, while the IEnumerable<TSource> Concat<TSource>() extension method expects two same types (IEnumerable<TSource>). The three TSource's must be the same.
Uncomment "// intA" in VS and you'll see the difference.
Anonymous objects will be equivalent types to other anonymous objects with the same property names and types declared in exactly the same order.
Assuming both
query
andquery2
from from the same contexts, you should be able to combine the two, provided they are queries of equivalent types.Your comment indicates that neither are of the same type.
query
returns objects of typeAnon<FileItem, Employee, int, string, string>
query2
returns objects of typeAnon<FileItem, Employee, int?, string, string>
.You cannot combine the two because they are of different types. You'll need to make sure that both queries return objects of the same type.
As you said previously in the comments, it seems that the two queries return different objects:
Query 1 (as per comment):
Query2 is
This is why
Concat
is giving you an error message complaining about invalid arguments.Are you missing any namespace? Normally I mark my .NET Project Properties to target .net 4.0 for vs 2010. I do not use .net 4.0 Client Profile.
Please make sure that the types of A, B and C is matches in both the query anonymous types. Also the order of A, B and C should also match in both queries.
The following example works like a charm.