指定的 LINQ 表达式包含对与不同上下文关联的查询的引用。
  9OBEU3KXBjcR 2023年11月02日 32 0


错误方式:

public List<EventInfoDetail> DetailEventInfo()
 {
     List<EventInfoDetail> EventInfoDetailInfo = null;
     
     using (StagingDBModelContainer container= new StagingDBModelContainer())
     {  
 using (DWDevDBEntities  DWEntities = new DWDevDBEntities())
        {

         List<EventInfoDetail> list = (from ei in DWEntities.EventInfo
                                       where ei.DeletedDate == null
                                       join dd in container.DataDictionary ei.EventNameID equals dd.PrimaryID into temp1
                                       from ddtemp in temp1.DefaultIfEmpty()
                                       join dp in DWEntities.DimPlaza on ei.PlazaCode equals dp.PlazaID into temp2
                                       from dptemp in temp2.DefaultIfEmpty()
                                       select new EventInfoDetail()
                                       {
                                           ID = ei.ID,
                                           SequenceNumber = 0,
                                           PlazaCode = ei.PlazaCode,
                                           PlazaName = dptemp.PlazaName,
                                           OperationName = dptemp.Owner,
                                           ZoneName = dptemp.ZoneName,
                                           StartDate = ei.StartDate,
                                           EndDate = ei.EndDate,
                                           EventNameID = ei.EventNameID,
                                           EventName = ddtemp.Value,
                                           Satus = ei.Satus,
                                           CreatedUser = ei.CreatedUser,
                                           CreatedDate = ei.CreatedDate,
                                           ModifiedUser = ei.ModifiedUser,
                                           ModifiedDate = ei.ModifiedDate,
                                           DeletedUser = ei.DeletedUser,
                                           DeletedDate = ei.DeletedDate
                                       }).ToList();

 if (list != null && list.Count() > 0)
 {
 EventInfoDetailInfo = list.ToList();
 }
        }  
     }

     return EventInfoDetailInfo;
 }

报错错误:指定的 LINQ 表达式包含对与不同上下文关联的查询的引用。 

 

 

正确方式:

 

public List<EventInfoDetail> DetailEventInfo()
  {
             List<EventInfoDetail> EventInfoDetailInfo = null;
             
             List<DataDictionary> DataDictionaryInfo = null;
             using (StagingDBModelContainer container = new StagingDBModelContainer())
             {
                 DataDictionaryInfo = (from dd in container.DataDictionary
                                       where dd.DeletedDate == null && dd.Type == 8
                                       select dd).ToList();
             }

             List<EventInfoDetail> list = null;
             using (DWDevDBEntities DWEntities  = new DWDevDBEntities())
             {
                                    list = (from ei in  DWEntities.EventInfo
                                               where ei.DeletedDate == null
                                               join dp in  DWEntities.DimPlaza on ei.PlazaCode equals dp.PlazaID into temp2
                                               from dptemp in temp2.DefaultIfEmpty()
                                               select new EventInfoDetail()
                                               {
                                                   ID = ei.ID,
                                                   SequenceNumber = 0,
                                                   PlazaCode = ei.PlazaCode,
                                                   PlazaName = dptemp.PlazaName
                                               }).ToList();
                 
             }


             var query = (from l in list
                         join dd in DataDictionaryInfo on l.EventNameID equals dd.PrimaryID into temp1
                         from ddtemp in temp1.DefaultIfEmpty()
                         select new EventInfoDetail()
                         {
                             ID = l.ID,
                             SequenceNumber = 0,
                             PlazaCode = l.PlazaCode,
                             PlazaName = l.PlazaName
                         }).ToList();


             if (query != null && query.Count() > 0)
             {
                 EventInfoDetailInfo = query.ToList();
             }

             return EventInfoDetailInfo;
         }

对于分库查询,解决思路是首先分别查询,再根据关联关系进行连接得到最终结果

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  4rNevj0bUzRn   2023年11月02日   47   0   0 加载取进程List
  gAerolsDGla4   2023年11月13日   19   0   0 svnListGo
  sX9JkgY3DY86   2023年11月13日   27   0   0 ideTextList
  sX9JkgY3DY86   2023年11月13日   28   0   0 bcget请求List
  C3KwzfU39uKz   2023年11月02日   20   0   0 IPList
9OBEU3KXBjcR