Linq查询问题

Sre*_*ree 1 c# asp.net linq-to-sql

我有一个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();
Run Code Online (Sandbox Code Playgroud)

但神秘的是我按升序获取数据,屏幕截图显示在这里

我不明白里面发生了什么.

Con*_*rix 5

在您执行分组后,将订单移至

(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();
Run Code Online (Sandbox Code Playgroud)

之所以如此,是因为group by result.Invoice_Number覆盖了之前的顺序.由于发票编号通常以日期顺序给出,因此它似乎在日期升序中.