我试图从租借表中删除电影,如果从列表框中选择它并且它正在工作但我还需要从rentingpeople表中删除连接到电影的数据
租赁表包含listobox和personid中的movieid,也可以在租借人员中找到,如果匹配则应该删除整行。
var search = (from g in db.Rentals where g.Movietitle == (string)lBfilmlista.SelectedValue select g).First();
foreach (var c in db.Rentingpeople where c.personid==search.personid).First();
{
db.Rentingpeople.DeleteObject(c);
}
db.Rentals.DeleteObject(search);
db.SaveChanges();
where c.personid==search.personid
处存在代码错误;预期
答案 0 :(得分:0)
问题在First
循环结束时foreach
,并且还将查询语法与方法语法混合。
所以你的代码应该是:
var search = (from g in db.Rentals where g.Movietitle == (string)lBfilmlista.SelectedValue select g).First();
var rowsToBeDeleted = db.Rentingpeople.Where(c=> c.personid==search.personid).ToList();
foreach (var item in rowsToBeDeleted)
{
db.Rentingpeople.DeleteObject(item);
}
db.Rentals.DeleteObject(search);
db.SaveChanges();