首页 > 解决方案 > C# - 返回 PayDate 最低但 ReconciliationDate 最高的记录

问题描述

所以我需要提取符合以下条件的记录:

PayDate 最低但 ReconciliationDate 最高的记录。可以看到这样的记录示例

public class Data {
    public string Name { get; set; }
    public DateTime PayDate { get; set; }
    public DateTime ReconciliationDate { get; set; }
}

--------------------------------------------------
| Name   | PayDate    | ReconciliationDate       |
--------------------------------------------------
| Andrew | 11/14/2018 | 07/01/2018               |
--------------------------------------------------
| Andrew | 10/14/2018 | 06/01/2018               |
--------------------------------------------------
| Andrew | 05/14/2018 | 08/01/2018               |
--------------------------------------------------
| Andrew | 05/14/2018 | 03/01/2018               |
--------------------------------------------------
| Andrew | 05/14/2018 | 10/01/2018               |

所以要返回的正确记录是#5

如何使用 C# 实现这一点?

到目前为止我有这个:

    var recordsNextCouponPaymentAfterLastOfMonth = externalInterestForSecurity.Where(aExternalRecord => aExternalRecord.PayDate > priorMonthEnd);

标签: c#linq

解决方案


您正在寻找 LINQ 方法OrderByThenBy

在你的情况下

.OrderBy(x => x.PayDate).ThenByDescending(x => x.ReconciliationDate).First

推荐阅读