【问题标题】:Group By & Sum Multiple Columns分组依据和对多列求和
【发布时间】:2019-11-13 18:27:30
【问题描述】:

我有以下 sql 查询,我想将其转换为 lambda 表达式:

SELECT TOP 10 SUM(Quantity * Price) AS Quantity, Category 
FROM InvoiceItem 
WHERE Unit = 'Hour' 
GROUP BY Category 
ORDER BY Quantity DESC

我尝试了很多东西,但我不明白这有什么问题:

var data = _db.InvoiceItem.Where(where => where.Unit == "Hour")
                          .GroupBy(group => new { group.Category })
                          .Select(group => new
                          {
                              Category = group.Key.Category,
                              Quantity = group.Sum(s => s.Quantity * s.Price)
                          })               
                          .OrderByDescending(ob => ob.Quantity)
                          .Take(10);

不幸的是,我不断收到以下错误:

Message = "数据为 Null。无法对 Null 值调用此方法或属性。"

这是我的模型:

namespace Accounts.Models
{

    public enum UnitList
    {
        Hour, Each, Km, Bag
    }

    public class InvoiceItem
    {

        public InvoiceItem()
        {
            Price = 0;
            Quantity = 1;          
            Unit = UnitList.Each.ToString();
            Display = false;
        }

        [Key]
        public int InvoiceItemID { get; set; }

        [Required]
        public int InvoiceID { get; set; }

        [Required]
        public int PersonID { get; set; }

        [Required]
        public Guid UserID { get; set; }

        [Required]        
        [DataType(DataType.Date)]
        //[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? Date { get; set; }

        [StringLength(50)]
        public string Category { get; set; }

        [StringLength(50)]
        public string Aircraft { get; set; }

        [Required]
        [StringLength(200)]
        public string Description { get; set; }

        [Required]
        [StringLength(20)]
        public string Unit { get; set; }     

        [Required]
        [DataType(DataType.Currency)]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")]
        public decimal Price { get; set; }

        [Required]
        [DefaultValue(1)]
        public decimal? Quantity { get; set; }

        [UIHint("Boolean")]
        public Boolean Display { get; set; }

        public virtual Invoice Invoice { get; set; }

        public virtual Person Person { get; set; }        
    } 


}

【问题讨论】:

    标签: c# sql linq lambda


    【解决方案1】:

    很可能您正在获取Categorynull 的数据。所以你需要在Where 中添加额外的条件。另外,你可以稍微简化一下GroupBy

    _db.InvoiceItem.Where(i => i.Unit == "Hour" && i.Category != null)
                          .GroupBy(i => i.Category)
                          .Select(i => new
                          {
                              Category = i.Key.Category,
                              Quantity = i.Sum(s => s.Quantity * s.Price)
                          })               
                          .OrderByDescending(i => i.Quantity)
                          .Take(10);
    

    【讨论】:

    • 我复制并粘贴了您的确切代码,我收到以下错误:'string' does not contain a definition for 'Category' and no accessible extension method 'Category' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) i.Key.Category 部分带有红色下划线。
    • OK 我通过更改为Category = i.Key, 修复了第一个字符串错误。但是我仍然收到空值错误。这似乎与乘法部分有关。如果我只使用 s.Quantity 或 s.Price 它可以工作,但是当两者相乘时它会失败。有什么想法吗?
    • @Reafidy 试试(s.Quantity ?? 0) * (s.Price ?? 0)
    • 我仅在 s.price 上收到以下编译错误:Operator '??' cannot be applied to operands of type 'decimal' and 'int'
    • 好的,所以这行得通Quantity = i.Sum(s => s.Price) 但这不是Quantity = i.Sum(s => s.Price * 1) 这似乎很奇怪。为什么乘以一会产生空值错误?
    猜你喜欢
    • 2020-04-23
    • 2015-05-17
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2012-01-03
    相关资源
    最近更新 更多