Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linq

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

like image 540
gan Avatar asked Aug 01 '11 15:08

gan


1 Answers

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.

like image 70
Brian Mains Avatar answered Nov 01 '22 12:11

Brian Mains