我想在MVC3项目中使用LINQ获取十进制值。通过LINQ选择十进制值
我该如何得到它?它看起来很容易的问题,但我不能得到一个单一的十进制值,而不是列表Product.aWeekPrice。
如何获得特定的threeDayPrice?越来越threeDayPrice后,它将被应用于如果条件给予不同的条件下,你可以在我下面的代码中看到:
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的数据类型是什么?它们是小数集合,还是小数? – McGarnagle 2012-04-02 04:43:32
这个问题与MVC无关。这是一个数据库问题,所以它是Entity Framework或Linq to Sql,不知道哪个 – 2012-04-02 05:07:13