首页 > 解决方案 > Linq - Linq 表达式不同的上下文错误

问题描述

我有 3 张桌子

- ERPEntry
- ERPEntryType
- ERPApp

我正在尝试使用以下查询从这 3 个表中获取数据,但出现错误:

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

var erpEntryInfo = (from s in ERPDB.ERPEntrys
                    JOIN t in ERPDB.ERPEntryTypes
                    on s.EntryTypeID equals t.EntryTypeID
                    join a in APPDB.ERPApps
                    on s.AppId equals a.AppId
                    where s.UserIDAdded == '250176'
                    select new ERPInfo
                    {
                       EntryId = s.EntryID,
                       EntryType = t.EntryTypeName,
                       ERPApp = a.ApplicationName,
                       DateAdded = s.DateAdded
                    }).OrderByDescending(d => d.DateAdded).Take(10).ToList();

我根据错误进行了搜索,并尝试将上述查询拆分为 2,如下所示。

var res = (from s in ERPDB.ERPEntrys
         join t in ERPDB.ERPEntryTypes
         on s.EntryTypeID equals t.EntryTypeID
         where s.UserIDAdded == '250176'
         select new {s.EntryTypeID, s.DateAdded, t.EntryTypeName, s.AppID }).OrderByDescending(d => d.DateAdded).Take(10).ToArray();

var y = (from a in APPDB.ERPApps
         join b in res on a.AppId equals //??//
         select new ERPInfo
         {
          EntryId = b.EntryID,
          EntryType = b.EntryTypeName,
          ERPApp = a.ApplicationName,
          DateAdded = b.DateAdded
         }).ToList();

我在上面的查询中遇到了访问 AppId 的问题,我进入了结果 res..我在上面的代码中用 //??// 评论

我能得到任何帮助吗?

标签: c#linq

解决方案


推荐阅读