2017-02-16 92 views
1

我有這個查詢返回一個IQueryable結果。排序IQueryable結果Linq

return supplierInvoices.Union(otherInvoices.Join(orders, invoice => invoice.OrderId, order => order.Id, (invoice, order) => new { Order = order, Invoice = (invoice as IReconcilableInvoiceEntity) })) 
      .GroupBy(m => m.Order) 
      .Where(m => m.Any()) 
      .Select(m => new KeyValuePair<Order, IQueryable<IReconcilableInvoiceEntity>>(m.Key, m.Select(n => n.Invoice).AsQueryable())); 

我需要這由CreatedOn列(datetime)降序排序。我試圖用這個查詢使用orderbydescending(a => a.CreatedOn)orderbydescending(a=> a.Key.CreatedOn),但仍然沒有給出預期的結果。所有幫助讚賞。謝謝!

編輯 - 我曾嘗試

return supplierInvoices.Union(otherInvoices.Join(orders, invoice => invoice.OrderId, order => order.Id, (invoice, order) => new { Order = order, Invoice = (invoice as IReconcilableInvoiceEntity) })) 
      .GroupBy(m => m.Order) 
      .Where(m => m.Any()) 
      .OrderByDescending(m => m.Key.CreatedOn) 
      .Select(m => new KeyValuePair<Order, IQueryable<IReconcilableInvoiceEntity>>(m.Key, m.Select(n => n.Invoice).AsQueryable())); 

這並不正確排序

闖闖

return supplierInvoices.Union(otherInvoices.Join(orders, invoice => invoice.OrderId, order => order.Id, (invoice, order) => new { Order = order, Invoice = (invoice as IReconcilableInvoiceEntity) })) 
      .GroupBy(m => m.Order) 
      .Where(m => m.Any()) 
      .Select(m => new KeyValuePair<Order, IQueryable<IReconcilableInvoiceEntity>>(m.Key, m.Select(n => n.Invoice).AsQueryable().OrderByDescending(m => m.CreatedOn))); 

它給出了一個錯誤。

+1

使用不工作的OrderByDescending編輯問題,以便我們可以看到您嘗試的內容。 – Esko

+0

@Esko:更新.. – Isuru

回答

1

如果要通過兩列命令,那麼你可以使用 「OrderByDescending(A => a.column1)」,其次是 「ThenBy(A => a.column2)」 得到想要的結果。