LINQ表达式包含对与不同上下文关联的查询的引用

时间:2014-10-02 19:54:50

标签: c# linq entity-framework

这是我的代码:

var myStrings = (from x in db1.MyStrings.Where(x => homeStrings.Contains(x.Content))
                    join y in db2.MyStaticStringTranslations on x.Id equals y.id
                    select new MyStringModel()
                    {
                        Id = x.Id,
                        Original = x.Content,
                        Translation = y.translation
                    }).ToList();

我得到的错误是指定的LINQ表达式包含对与不同上下文关联的查询的引用。我知道问题是我尝试从db1和db2访问表,但我该如何解决这个问题?

3 个答案:

答案 0 :(得分:6)

  

MyStrings是一张小桌子

在内存中加载过滤的MyStrings,然后使用LINQ:

MyStaticStringTranslations加入
// Read the small table into memory, and make a dictionary from it.
// The last step will use this dictionary for joining.
var byId = db1.MyStrings
    .Where(x => homeStrings.Contains(x.Content))
    .ToDictionary(s => s.Id);
// Extract the keys. We will need them to filter the big table
var ids = byId.Keys.ToList();
// Bring in only the relevant records
var myStrings = db2.MyStaticStringTranslations
    .Where(y => ids.Contains(y.id))
    .AsEnumerable() // Make sure the joining is done in memory
    .Select(y => new {
        Id = y.id
        // Use y.id to look up the content from the dictionary
    ,   Original = byId[y.id].Content
    ,   Translation = y.translation
    });

答案 1 :(得分:1)

你是对的,db1和db2不能在同一个Linq表达式中使用。 x和y必须在此过程中加入,而不是由Linq提供者加入。试试这个:

var x = db1.MyStrings.Where(xx => homeStrings.Contains(xx.Content)).ToEnumerable();

var y = db2.MyStaticStringTranslations.ToEnumerable();

var myStrings = (from a in x
                 join b in y on x.Id equals y.id
                    select new MyStringModel()
                    {
                        Id = x.Id,
                        Original = x.Content,
                        Translation = y.translation
                    }).ToList();

有关详细信息,请参阅此答案:The specified LINQ expression contains references to queries that are associated with different contexts

dasblinkenlight的答案有一个比这更好的整体方法。在这个答案中,我试图最小化与原始代码的差异。

答案 2 :(得分:0)

我也面临同样的问题: "指定的LINQ表达式包含对与不同上下文关联的查询的引用。" 这是因为它无法一次连接到两个上下文,因此我找到了如下解决方案。 在这个示例中,我想列出具有所有者名称的彩票卡,但具有所有者名称的表位于另一个Database.So我制作了两个上下文DB1Context和DB2Context.and编写如下代码:

 var query = from lc in db1.LotteryCardMaster
             from om in db2.OwnerMaster
             where lc.IsActive == 1
             select new
                        {
                            lc.CashCardID,
                            lc.CashCardNO,
                            om.PersonnelName,
                            lc.Status

                        };

AB.LottryList = new List<LotteryCardMaster>();
            foreach (var result in query)
            {
                AB.LottryList.Add(new LotteryCardMaster()
                {
                    CashCardID = result.CashCardID,
                    CashCardNO = result.CashCardNO,
                    PersonnelName =result.PersonnelName,
                    Status = result.Status

                });
            }

但是这给了我上面的错误,所以我找到了另外一种方法来从不同的数据库中执行两个表的连接。这种方式如下所示。

var query = from lc in db1.LotteryCardMaster
            where lc.IsActive == 1
            select new
                        {
                            lc.CashCardID,
                            lc.CashCardNO,
                            om.PersonnelName,
                            lc.Status

                        };

AB.LottryList = new List<LotteryCardMaster>();
            foreach (var result in query)
            {
                AB.LottryList.Add(new LotteryCardMaster()
                {
                    CashCardID = result.CashCardID,
                    CashCardNO = result.CashCardNO,
                    PersonnelName =db2.OwnerMaster.FirstOrDefault(x=>x.OwnerID== result.OwnerID).OwnerName,
                    Status = result.Status

                });
            }