我是sql-linq的新手,我正在尝试使用他们的共同ID(即motherid)加入两个表。我已经这样做但是当我尝试将返回的值转换为列表时,它会抛出一个异常,说“查询包含对在不同数据上下文中定义的项的引用”。这是代码。
var todaySecondVisitProfile = (from a in _maternalvisitvaluedb.Value
join b in _maternalcarevaluedb.Value on a.MotherId equals b.MotherID
where (DateTime)a.SecondVisit.Date == DateTime.Now.Date
select new
{
FirstName = b.FirstName,
LastName = b.LastName,
PhoneNo = b.PhoneNo
}).ToList();
如果我无法将结果转换为列表,我该如何访问我的结果? tnx的帮助。
答案 0 :(得分:3)
我认为您正在尝试在两个不同的数据库上执行Linq。如果是这样,你应该这样:
var firstQuery = (from s in _maternalvisitvaluedb.Value select s).ToList();
var secondQuery = (from t in _maternalcarevaluedb.Value select t).ToList();
var result = (from s in firstQuery
join k in secondQuery
on s.MotherId equals k.MotherId
where (DateTime)s.SecondVisit.Date == DateTime.Now.Date
select s).ToList();