【问题标题】:C# - Return record which has the lowest PayDate but also the highest ReconciliationDate [duplicate]C# - 返回具有最低支付日期但也是最高对帐日期的记录[重复]
【发布时间】:2018-11-14 22:32:22
【问题描述】:

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

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

【问题讨论】:

  • 您可以使用OrderByOrderByDescendingThenBy 和/或ThenByDescending 对列表进行排序,然后取第一行。见this question

标签: c# linq


【解决方案1】:

您正在寻找 LINQ 方法 OrderByThenBy

在你的情况下

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

【讨论】:

  • 选择我认为的第一个
  • @CamiloTerevinto 你是什么意思?这正是他所需要的。
猜你喜欢
  • 2019-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多