LINQ is used to pick decimal values.

.net entity-framework linq linq-to-sql

Question

In an MVC3 project, I'm attempting to use LINQ to get a decimal number.

Where can I get it? It seems like a simple query, but I am unable to get the single decimal number for "Product" that is not shown. aWeekPrice.

How can I get the specific threeDayPrice? As you can see in my code below, threeDayPrice will be utilized in the if condition after being obtained to provide various circumstances.

    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;
        }        
    }
1
3
4/23/2012 9:37:12 AM

Accepted Answer

Use .Single() to acquire the value if your query always returns only one result.decimal rather than a group of decimals.

    decimal threeDayPrice = (from cartItems in db.Cart
                          where cartItems.cartId == ShoppingCartId
                             select (decimal)cartItems.Product.threeDayPrice).Single();

    decimal aWeekPrice = (from cartItems in db.Cart
                             where cartItems.cartId == ShoppingCartId
                             select (decimal)cartItems.Product.aWeekPrice).Single();

Use First()/FirstOrDefault() instead if it's possible that the query may return more than one or zero entries.

5
4/2/2012 5:38:31 AM

Popular Answer

According to that error notice, cartItems.Product.aWeekPrice is a collection rather than a single item. Therefore, all you have to do is choose the thing you need. For instance:

decimal threeDayPrice = (from cartItems in db.Cart
                     where cartItems.cartId == ShoppingCartId
                     select (decimal)cartItems.Product.threeDayPrice).FirstOrDefault();

should do so by choosing the first item in the "threeDayPrice" column, but this will still result in the error notice being removed.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow