C# 如何内部连接来自不同数据上下文的表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1537805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inner join tables from different Data Context?
提问by jinsungy
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.
我怎样才能解决这个问题?任何帮助表示赞赏。谢谢。
回答by Greg D
You don't. The data contexts may have inconsistent views of the database.
你没有。数据上下文可能具有不一致的数据库视图。
回答by KristoferA
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...
L2S 数据上下文使用类上的属性,因此如果您在另一个数据上下文上使用 GetTable 而不是表所附加的数据上下文,则只会从类 def 中获取表、列等属性,并像使用它一样使用它您在查询中使用的 DC...
回答by Henry
Another solution is change the result to List().
另一种解决方案是将结果更改为 List()。
var query = (from a in dc1.TableA
join b in dc2.TableB on a.id equals b.id
select new { a, b }).ToList()