How to inner join tables from different Data Context?

jinsungy picture jinsungy · Oct 8, 2009 · Viewed 50.6k times · Source

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.

Answer

KristoferA picture KristoferA · Oct 8, 2009

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...