排序IQueryable结果Linq

时间:2017-02-16 05:52:55

标签: c# linq

我有这个查询返回一个IQueryable结果。

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 个答案:

答案 0 :(得分:1)

如果您想按两列排序,则可以使用&#34; OrderByDescending(a =&gt; a.column1)&#34;其次是 &#34;的 ThenBy(A =&GT; a.column2)&#34;获得理想的结果。