我想在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