LINQ expression that contains references to queries that are associated with different contexts

旧街凉风 提交于 2019-12-07 10:29:14

问题


I have this query:

var list = (from t1 in context1.SomeTable
            join t2 in context2.SomeTable on t1.ID equals t2.ID
            where //some where clause
            select new { t1.SomeField, t2.SomeField }).ToList());

I will get this error when this query tries to execute:

The specified LINQ expression contains references to queries that are associated with different contexts.

  1. Why is this not allowed with LINQ to Entities?
  2. Is it still possible with LINQ to Entities in another way?
  3. What would be a work around for this?

回答1:


I'd imagine it's because the statement you're building up is converted to SQL behind the scenes and run on the database. Because different context could come from different databases or even different servers, there's no guarantee that the data in context2 is available to context1 when the server is being queried.

You could return the data from each context and convert to IEnumerable and then perform standard linq queries then but you've got a clear overhead of data transfer and in-memory processing that would have otherwise been performed by the database engine.



来源:https://stackoverflow.com/questions/7024100/linq-expression-that-contains-references-to-queries-that-are-associated-with-dif

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