I have 2 var/objects, retrieving thru these 2 functions:
private IQueryable<Project> SelectAll_1(...)
{
return query;
}
class Project is:
private int ID;
private string col1;
private string col2;
private string col3;
and another:
private IQueryable<Project_test> SelectAll_2(...)
{
return query;
}
where POCO of is:
private string ID_inString;
private string col1;
private string col2;
private string col3;
and i need to perform union on both of them,
var P2 = SelectAll_2(...);
var P1 = SelectAll_1(...);
var P3 = P2.Union(P1);
but i get an error, mentioning:
The type arguments for method ‘System.Linq.Queryable.Union(System.Linq.IQueryable, System.Linq.Expressions.Expression>)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.
i saw some people solve it thru’ anonymous type, but i not sure how it works. Anyone has any idea?
You can’t union two different types, unless one inherits from the other (for example, you could potentially find a union of
IEnumerable<object>andIEnumerable<string>, although that would rarely be useful).Now in your case, it sounds like
ProjectandProject_testshould really be one type, if the various properties have the same meaning. Currently the ID properties have different types – but is that really necessary or desirable? If they’re both identifiers with the same scope, it makes sense to store them in the same representation. If the properties don’t have the same meaning, you shouldn’t be forming a union between them at all. If they do have the same meaning, you should try to make both sequences use the same type.You could use anonymous types for this, in this way:
Or you could just use one of the existing types:
It’s not really obvious which of these is a better idea – but I’d go back to trying to reconcile the two types if possible, at which point you have no problems anyway.