Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inner join tables from different Data Context? [duplicate]

I have two tables from two different Data Contexts. Although both tables are from the same database, two separate datacontexts exist.

Error message:

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

How can I get around this? Any help is appreciated. Thanks.

like image 750
jinsungy Avatar asked Oct 08 '09 13:10

jinsungy


3 Answers

If your code does something along the lines of:

from a in dc1.TableA
join b in dc2.TableB on a.id equals b.id
select new { a, b }

...just change it to:

from a in dc1.TableA
join b in dc1.GetTable<TableB>() on a.id equals b.id
select new { a, b }

The L2S datacontext uses the attributes on the class, so if you use GetTable on another datacontext than the one the table is attached to it will just pick up the table, column, etc attributes from the class def and use it as if it was part of the DC you're using in the query...

like image 130
KristoferA Avatar answered Oct 10 '22 03:10

KristoferA


Another solution is change the result to List().

var query = (from a in dc1.TableA 
            join b in dc2.TableB on a.id equals b.id 
            select new { a, b }).ToList()
like image 43
Henry Avatar answered Oct 10 '22 05:10

Henry


You don't. The data contexts may have inconsistent views of the database.

like image 33
Greg D Avatar answered Oct 10 '22 04:10

Greg D