【问题标题】:select decimal value through LINQ通过 LINQ 选择十进制值
【发布时间】: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


【解决方案1】:

如果您的查询总是只返回一个值,请使用.Single() 将其作为decimal 而不是小数集合。

    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();

如果查询可能返回多于一个或零个元素,请改用First()/FirstOrDefault()

【讨论】:

  • 感谢您的回复,但是当我输入 .Single() 或 First() 或 FirstOr Default() 时,在公共十进制 GetTotal(十进制价格)上发生错误。它是说'并非所有代码路径都返回一个值'。这是什么意思?
  • 这不是 LINQ 错误。在您的方法中,当 (price != threeDayPrice and price != aWeekPrice) 时没有返回值(没有执行它的 if 语句)。您应该在方法末尾添加return decimal.Zero;
  • 谢谢你的好意!谢谢。
【解决方案2】:

该错误消息表明 cartItems.Product.aWeekPrice 是一个集合,而不是一个集合。所以,你只需要选择你需要的任何项目。例如:

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

应该删除错误消息,但它会通过选择“threeDayPrice”字段中的第一项来完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多