如何将此sql服务器转换为linq代码 [英] How to covert this sql server to linq code
问题描述
请问我如何将其转换为LINQ
Can you please so me how to convert this to LINQ
select q.Quantity - 1 from dbo.Quantity q
join dbo.OrderDetails o on q.OrderDetailId = o.OrderDetailId
join dbo.Doctors d on o.DoctorsId = d.DoctorsId
where q.Quantity > 0
我以为是这样,但是我被卡住了.
I thought it was something like this, yet I am stuck.
var od = (from q in _db.Quantities
join o in _db.OrderDetails on q.OrderDetailId equals
o.OrderDetailId
join d in _db.Doctors on o.DoctorsId equals d.DoctorsId
where q.Quantity > 0
select new {sum = q.Quantity - 1});
我需要做的是,当用户提交表单时,它会从数量表中减去,如果数量表的列数量大于0则为负1,则订单明细ID和医生ID等于数量表.每次提交时减去1直到零.或购买更多.
what is I need to happen is when a user submit a form it subtract form the quantity table where the order details id and doctor id equals if the quantities table column quantity is greater then 0 then minus 1. Every time the submit subtract one until zero or the buy more.
推荐答案
var quantityDetails =
(Quantity) (from q in _db.Quantities
join o in _db.OrderDetails on q.OrderDetailId equals o.OrderDetailId
join d in _db.Doctors on o.DoctorsId equals d.DoctorsId
where q.Quantity1 > 0
select q).Single();
quantityDetails.Quantity1 = quantityDetails.Quantity1 - 1;
_db.SaveChanges();
必须进行更改,因为必须确保仅连接到用户登录名,并且将表单更改为firstordefault,因为
获取错误代码序列包含多个元素
had to change because had to make sure connected to only the user login and change form single to firstordefault because
getting error code sequence contains more than one element
var quantityDetails = (from u in _db.aspnet_Users
where u.UserName == HttpContext.Current.User.Identity.Name
join d in _db.Doctors on u.UserId equals d.UserId
join o in _db.OrderDetails on d.DoctorsId equals o.DoctorsId
join q in _db.Quantities on o.OrderDetailId equals q.OrderDetailId
where q.Quantity1 > 0
select q).FirstOrDefault();
if (quantityDetails != null) quantityDetails.Quantity1 = quantityDetails.Quantity1 - 1;
_db.SaveChanges();
这篇关于如何将此sql服务器转换为linq代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!