I have "Products table" with following fields
PID int,
product_name varchar(),
product_price int
我有“产品表”,其中包含以下字段:PID int,product_name varchar(),product_price int
"Cart table" with following fields
cart_ID
user_id
PID
“购物车表”,包含以下字段:cart_ID user_id PID
So I want to display cart items of logged in user
For example if user_ID=100 is logged in , then only his cart items should be displayed to him, with all the product details.
Am using asp.net with entity framework
所以我想,如果USER_ID = 100登录,那么只有他的车中的物品应显示给他,所有的产品细节显示的登录用户例如车中的物品。我使用asp.net与实体框架
public ActionResult Cart()
{
Products pro = new Products();
Cart cart =new Cart();
var productID=db.cartDetails.Where(pid=>pid.productId==cart.productId && cart.user_id==session["user_ID"]);
return View(db.productsDetails.Where(pid => pid.productId == productID));
}
Now problem arise, "ProductID" being var type i cannot compare it with "pid=>pid.productid".
How to store productid in int List, so that i can compare it with individual productid from product table to show product details?
现在出现问题,“ProductID”是var类型我不能将它与“pid => pid.productid”进行比较。如何在int List中存储productid,以便我可以将它与产品表中的单个产品进行比较以显示产品详细信息?
3 个解决方案
#1
0
try to convert it before check
尝试在检查之前转换它
EDIT db.cartDetails.Where(pid=>pid.productId==cart.productId
this will return object of product or something so need to change your code in to
这将返回产品对象或其他东西,因此需要更改您的代码
var product = db.cartDetails.Where(pid=>pid.productId==cart.productId).FirstOrDefault();
if(product!= null){
return View(db.productsDetails.Where(pid => pid.productId == product.productID));
}
#2
0
db.cartDetails.Where(pid=>pid.productId==cart.productId) returns a `WhereListIterator<CartDetail>` object.
So you'll need to to do something like -
所以你需要做一些像 -
var productID=db.cartDetails.Where(pid=>pid.productId==cart.productId).FirstOrDefault();
return View(db.productsDetails.Where(pid => pid.productId == productID.productID));
#3
0
You can try this
你可以试试这个
Edit :
First thing you can not compare directly session["user_ID"] with int datatype field as you are doing in you comment cart.user_id==session["user_ID"]
follow updated code.
首先你无法直接将session [“user_ID”]与int数据类型字段进行比较,就像你在评论cart.user_id == session [“user_ID”]一样,按照更新的代码进行比较。
var userID = Convert.ToInt32(Session["user_ID"]); //And please check first session is null or not than go ahead.
//Here as you said now you can compare productID and userId as follow.
//And if these both conditions will be satisfied than you will get cartDetails table first record otherwise it will return null value
var cartDetail = db.cartDetails.Where(pid => pid.productId == cart.productId && pid.user_id == userID).FirstOrDefault();
if(cartDetail != Null)
{
return View(db.productsDetails.Where(pid => pid.productId == cartDetail.productID));
}
FirstOrDefault() Returns the first element of a sequence, or a default value if the sequence contains no elements.
FirstOrDefault()返回序列的第一个元素,如果序列不包含元素,则返回默认值。
My First line code return you int
productID if it is not null.
我的第一行代码返回int productID,如果它不为null。
#1
0
try to convert it before check
尝试在检查之前转换它
EDIT db.cartDetails.Where(pid=>pid.productId==cart.productId
this will return object of product or something so need to change your code in to
这将返回产品对象或其他东西,因此需要更改您的代码
var product = db.cartDetails.Where(pid=>pid.productId==cart.productId).FirstOrDefault();
if(product!= null){
return View(db.productsDetails.Where(pid => pid.productId == product.productID));
}
#2
0
db.cartDetails.Where(pid=>pid.productId==cart.productId) returns a `WhereListIterator<CartDetail>` object.
So you'll need to to do something like -
所以你需要做一些像 -
var productID=db.cartDetails.Where(pid=>pid.productId==cart.productId).FirstOrDefault();
return View(db.productsDetails.Where(pid => pid.productId == productID.productID));
#3
0
You can try this
你可以试试这个
Edit :
First thing you can not compare directly session["user_ID"] with int datatype field as you are doing in you comment cart.user_id==session["user_ID"]
follow updated code.
首先你无法直接将session [“user_ID”]与int数据类型字段进行比较,就像你在评论cart.user_id == session [“user_ID”]一样,按照更新的代码进行比较。
var userID = Convert.ToInt32(Session["user_ID"]); //And please check first session is null or not than go ahead.
//Here as you said now you can compare productID and userId as follow.
//And if these both conditions will be satisfied than you will get cartDetails table first record otherwise it will return null value
var cartDetail = db.cartDetails.Where(pid => pid.productId == cart.productId && pid.user_id == userID).FirstOrDefault();
if(cartDetail != Null)
{
return View(db.productsDetails.Where(pid => pid.productId == cartDetail.productID));
}
FirstOrDefault() Returns the first element of a sequence, or a default value if the sequence contains no elements.
FirstOrDefault()返回序列的第一个元素,如果序列不包含元素,则返回默认值。
My First line code return you int
productID if it is not null.
我的第一行代码返回int productID,如果它不为null。