【发布时间】:2012-04-02 04:35:27
【问题描述】:
我正在尝试在 MVC3 项目中使用 LINQ 获取十进制值。
我如何获得它?看起来很简单的问题,但我无法得到单个十进制值而不是 Product.aWeekPrice 列表。
如何获得特定的threeDayPrice?获得 threeDayPrice 后,它将在 if 条件中用于给出不同的条件,如下面的代码所示:
public decimal GetTotal(decimal price)
{
// Multiply product price by count of that album to get
// the current price for each of those product in the cart
// sum all product price totals to get the cart total
//In select part, I have got error, 'cannot convert type
//'System.Linq.IQueryable<decimal>' to 'decimal'
decimal threeDayPrice = (from cartItems in db.Cart
where cartItems.cartId == ShoppingCartId
select (decimal)cartItems.Product.threeDayPrice);
decimal aWeekPrice = (from cartItems in db.Cart
where cartItems.cartId == ShoppingCartId
select (decimal)cartItems.Product.aWeekPrice);
if (price == threeDayPrice)
{
decimal? total = (from cartItems in db.Cart
where cartItems.cartId == ShoppingCartId
select (int?)cartItems.count * cartItems.Product.threeDayPrice).Sum();
return total ?? decimal.Zero;
}
else if (price == aWeekPrice)
{
decimal? total = (from cartItems in db.Cart
where cartItems.cartId == ShoppingCartId
select (int?)cartItems.count * cartItems.Product.aWeekPrice).Sum();
return total ?? decimal.Zero;
}
}
【问题讨论】:
-
threeDayPrice/aWeekPrice 的数据类型是什么?它们是小数的集合,还是只是小数?
-
这个问题与MVC无关。这是一个数据库问题,所以它要么是实体框架,要么是 Linq to Sql,不确定是哪个
标签: .net linq entity-framework linq-to-sql