如何使用实体框架和 linq 编写此 sql 查询
本文关键字:sql 查询 linq 何使用 实体 框架 | 更新日期: 2023-09-27 18:35:53
我可以在实体框架中使用相同的概念编写下面提到的查询吗
update o set o.Name='NewName'
from Organization o
Inner join Guardian g on o.OrgRowId=g.OrgRowId
where g.IsEnabled=1 and g.OrgRowId=1
请指导!!
更新
根据要求,这就是我能够使用连接编写选择查询的内容,但无法在同一查询中进行更新
var query = from o in Organizations
join g in Guardians on o.OrgRowId equals g.OrgRowId
where g.IsEnabled && g.GuardianRowId==1
select o;
更新 1Kilanny,我尝试过你像这样查询和更新我的查询,但它不起作用,我正在 Linqpad 中运行这些查询
using (var context = new DbEntities())
{
var query = from o in Organizations
join g in Guardians on o.OrgRowId equals g.OrgRowId
where g.IsEnabled && g.GuardianRowId == 1
select o;
foreach (var item in query)
{
item.Name="New Organization Name";
context.SaveChanges();
}
query.Dump();
}
var query = from o in Organizations
join g in Guardians on o.OrgRowId equals g.OrgRowId
where g.IsEnabled && g.GuardianRowId==1
select o;
foreach (var item in query)
item.Name="NewName";
entities.SaveChanges();