Linq查询问题

时间:2011-05-04 19:39:18

标签: c# asp.net linq-to-sql

我有一个linq查询,它将CustomersRecord表中显示的数据显示为follows。现在,我将发票号和交易日期的数据分组,并按照交易日期的降序显示数据。这是我用来实现的以下查询。

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

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

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

1 个答案:

答案 0 :(得分: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();

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