我正在尝试使用视图和linq一起添加一些值这是下面的代码

var getProducts = from p in Entity.Products
                  join od in getOrderDetails on p.id equals od.productId into proDetails
                  orderby proDetails.Sum(q => q.quantity) descending
                  select new Common.Views.ProductQuantitySold()
                  {
                      productId = p.id,
                      productName = p.name,
                      productDesc = p.description,
                      qtySold = proDetails.Sum(q => q.quantity)
                  };
return getProducts.Take(10).AsQueryable();

qtySold = proDetails.Sum(q => q.quantity)行中,我得到了cast值错误。我知道它是关于空或零的,但是我应该如何实现它呢?

最佳答案

尝试替换以下行:

qtySold = proDetails.Sum(q => q.quantity)


qtySold = proDetails.Sum(q => (int?)q.quantity) ?? 0

09-25 20:48