Linq查询问题
本文关键字:问题 查询 Linq | 更新日期: 2023-09-27 17:54:37
我有一个linq查询,它显示CustomersRecord表中存在的数据如下所示。现在我对发票号和交易日期的数据进行分组,并按照交易日期降序排列数据。下面是我用来实现这个目标的查询。
(from result in db.CustomersRecords
orderby result.Date_Of_Transaction.Value descending
group result
by new { result.Invoice_Number, result.Date_Of_Transaction } into intermediateResult
select new { InvoiceNumber = intermediateResult.Key.Invoice_Number, DateOfTransaction = intermediateResult.Key.Date_Of_Transaction, TotalAmount = intermediateResult.Sum(result => result.Total_Amount) }).ToList();
但不可思议的是,我得到的数据是升序的,屏幕截图显示在这里
我不明白里面发生了什么
将排序移到
(from result in db.CustomersRecords
group result
by new { result.Invoice_Number, result.Date_Of_Transaction } into intermediateResult
orderby intermediateResult.Key.Date_Of_Transaction descending
select new { InvoiceNumber = intermediateResult.Key.Invoice_Number, DateOfTransaction = intermediateResult.Key.Date_Of_Transaction, TotalAmount = intermediateResult.Sum(result => result.Total_Amount) }).ToList();
之所以会这样,是因为分组看重结果。Invoice_Number重写了之前的订单。由于发票号码通常按日期顺序给出,因此看起来是按日期升序排列的。