以下是我在数据库中的一些表格
用户( UserEmail ,...)
蛋糕( CakeId ,姓名,股票,......)
购物车( UserEmail , CakeId ,...)
如何使lambda表达式与SQL查询匹配:
UPDATE Cake
SET Stock = Stock - 1
WHERE CakeId IN (SELECT CakeId FROM Cart WHERE UserEmail = "someone@example.com")
在实体框架中?
基本上,我想将当前登录用户购物车中存在的所有蛋糕库存减少1个。
答案 0 :(得分:0)
试试这样:
var cakes = (from c in dataContext.Cakes
join ca in dataContext.Carts on c.cakeId equals ca.cakeId
where ca.UserEmail = "someone@example.com"
select c);
foreach(Cake c in cakes)
{
c.Stock--;
}
答案 1 :(得分:0)
你可以获得股票,并使用foreach循环更新它,
//begin transaction
var context = DBcontext.Database.BeginTransaction();
//Select cakes that contains useremail "" in their carts
var cakes = DBcontext.Cakes.where(c => c.Carts.Any(x => x.UserEmail == "someone@example.com"));
// Loop through
foreach (var cake in cakes)
{
cake.Stock--;
//then update context
DBcontext.Entry(cake).State = EntityState.Modified;
}
//Then save changes
DBcontext.SaveChanges();
//then commit the transaction
context.commit();
答案 2 :(得分:0)
免责声明:我是该项目的所有者Entity Framework Plus
此库具有您正在寻找的批量更新功能
以下是如何使用它的示例。
Flowable.empty().doOnSubscribe { Log.d(TAG, "IS EMPTY") }
所有行都将在不加载内存中的实体的情况下进行更新。