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

僤鯓⒐⒋嵵緔 提交于 2020-01-10 19:34:34

问题


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.


回答1:


Oh Stackoverflow how you provide!!

Simulating Cross Context Joins--LINQ/C#




回答2:


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




回答3:


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




回答4:


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()



回答5:


Why don't you just include the necessary tables in each context?



来源:https://stackoverflow.com/questions/1537805/how-to-inner-join-tables-from-different-data-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!