错误方式:
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;
}
对于分库查询,解决思路是首先分别查询,再根据关联关系进行连接得到最终结果