需要检查数据库,以便查看是否购买了商品 [英] Need check the database so see if an item was purchase
问题描述
我正在尝试一个if/else.我需要首先检查数据库order_details表以查看数量是否为零.如果大于零,请转到提交页面,否则请转到主页.
这就是我所拥有的.
I am trying to do an if/else. I need to first check the database order_details table to see if the quantity is zero. If greater than zero go to submission page else go to home page.
This is what I have.
public ActionResult Patients()
{
//query database for userId RoleId and Purchase product.
var query = (from u in db.aspnet_Users
where u.UserName == 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
select o).FirstOrDefault().Quantity;
//check against the quantity table for valid purchase.
if (query > 0)
{
return View();
}
else
{
return RedirectToAction("Index", "Home");
}
}
如果订单明细"数量列为零,则会崩溃.
我登录时使用的任何一个在我的数据库的数量"列中都有一个数字即可.
我希望我能解释这一权利.要在用户单击选项卡时转到患者提交页面,需要查询数据库以查看是否有购买服务.
If the Order Details quantity column is zero it crashes.
Any one I Log on with that has a number in the quantity column in my database it works.
I hope I am explaining this right. To go to the patients submit page when a user clicks on the tab it need to query the database to check to see if purchase services.
推荐答案
<如果没有项目,则c0>方法返回
null
.
在使用它之前,您必须检查从FirstOrDefault
方法返回的值.类似于以下内容:
You have to check the value that is returned from from the FirstOrDefault
method, before using it. Something like the following:
var orderDetail = (from u in db.aspnet_Users
where u.UserName == 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
select o).FirstOrDefault();
var query = (orderDetail != null) ? orderDetail.Quantity : 0;
这篇关于需要检查数据库,以便查看是否购买了商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!