Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Can you please so me how to convert this to LINQ

SQL
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.

C#
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});


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.
Posted
Updated 26-Mar-12 21:20pm
v2
Comments
Herman<T>.Instance 27-Mar-12 4:10am    
what error do you get?
Herman<T>.Instance 27-Mar-12 5:24am    
what happens if you change
select new {sum = q.Quantity - 1});
to
select sum = q.Quantity - 1); ?
postonoh 27-Mar-12 9:30am    
What I really need is an update on the table here is the SQL statement

update Quantity
set Quantity = (
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 )

1 solution

C#
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();


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

C#
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();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900