Alex Rivera | Logout

The query contains references to items defined on a different data context

Asked 2011-08-01T15:16:13.403
9

I have 2 var from same DBs, after union:

var projectedP1 = P1.Select(x => new Project_test { 
                                     ID_inString = x.ID.ToString(), 
                                     col1 = x.col1, 
                                     col2 = x.col2, 
                                     col3 = x.col3 });
var union = projectedP1.Union(P2);

when P1 alone or P2 alone, everything is fine But when 2 is union, i get this in run-time:

The query contains references to items defined on a different data context.

I tried this similar post, but dun understand. ANyone has any idea?

The specified LINQ expression contains references to queries that are associated with different contexts

Edit
Report

1 Answer

13

You can never join objects from two different contexts; because the union is compiled into a database query, it wouldn't know how to execute it since multiple DB's are not supported. The best you can do is to call ToList() on each query separately, which will execute the database queries, and do a LINQ-to-Objects union. This is entirely an iterative process.

answered 2011-08-02T14:56:06.573

Your Answer