我正在用 C# 编写一个连接多个表的 Servicestack 更新查询。我已经搜索了网络并浏览了所有相关文档和网站,但无法找到有关使用联接查询进行更新的足够详细信息。
我想通过连接三个不同的表和 where 子句来更新一个表。我已经尝试了实体框架的所有方法,但无法做到。我可以通过 Db.ExecuteSql(SqlQuery) 来完成,但我想通过实体框架来完成。我的查询如下。
我想更新 HeartRate 表与 PatientDetails、DeviceSession、PatientSession 和 HeartRate 表连接的 UpdateStatus,其中子句是 HeartRate.timestamp = ‘@starttime’ 和 PatientId = ‘@PatientId’
Sql查询=
UPDATE HeartRate SET UpdateStatus = 1
WHERE HeartRateID IN ( SELECT hr.HeartRateID
FROM PatientDetails pd join PatientSession ps on pd.PatientDetailsId = ps.ByPatientId
join DeviceSession ds on ps.PatientSessionId = ds.ByPatientSessionId join HeartRate hr on ds.DeviceSessionID = hr.ByDevSessionID
WHERE
pd.PatientId = '@PatientId'
AND
hr.Timestamp = '@starttime'
order by hr.Timestamp Asc )
我需要类似下面的东西(这是错误的且不完整的)。
Db.UpdateOnly(
new HeartRate { UpdateStatus = 1 },
ps => new { ps.UpdateStatus },
.SqlJoinBuilder<DeviceSession, PatientSession>((ds2, ps) => ds2.ByPatientSessionId == ps.PatientSessionId)
.Join<PatientSession, PatientDetails>((ps2, pd) => ps2.ByPatientId == pd.PatientDetailsId)
.Where<HeartRate, PatientDetails>((lthr2, pd2) => (lthr2.Timestamp == @starttime) && pd2.PatientId == PatientId)
.OrderBy(lthr2 => lthr2.Timestamp));
//试试这个
var model = from v in Db.HeartRate
where
(
from pd in Db.PatientDetails
join ps in Db.PatientSession on pd.PatientDetailsId equals ps.ByPatientId
join ds in Db.DeviceSession on ps.PatientSessionId equals ds.ByPatientSessionId
join hr in Db.HeartRate on ds.DeviceSessionID equals hr.ByDevSessionID
where pd.PatientId == PatientId && hr.Timestamp == starttime
select new { HeartRateID = hr.HeartRateID }
).ToList().Contains(v.HeartRateID)
select v;
foreach (var item in model)
{
item.UpdateStatus = 1;
Db.SaveChanges();
}