Linq获取每个元素的最新日期(或最大值)

时间:2017-08-10 04:38:38

标签: c# linq

我有一张表格,其中包含帐户余额的历史记录。我想写一个Linq查询,返回每个帐户的最后余额。

此时我使用此查询:

var lastDate = AccountBalances.Max(y => y.BalanceDate);

var listOfBalance = AccountBalances.Where(y => y.BalanceDate == lastDate).ToList();
listOfBalance.Dump(); // FYI I use LinqPad

但这不是正确的,因为我不会每天执行每个帐户的计算。我可以为每个帐户设置不同的最后余额。

示例:

AccountNumber   |  BalanceDate  |  Value
----------------------------------------
1234-0001       |  2017-08-08   |  100
1234-0002       |  2017-08-08   |  250
1234-0003       |  2017-08-08   |  500
1234-0004       |  2017-08-08   |  150
1234-0001       |  2017-08-09   |  110
1234-0002       |  2017-08-09   |  230
1234-0003       |  2017-08-09   |  400
1234-0001       |  2017-08-10   |  120
1234-0002       |  2017-08-10   |  210

我的查询返回此

AccountNumber   |  BalanceDate  |  Value
----------------------------------------
1234-0001       |  2017-08-10   |  120
1234-0002       |  2017-08-10   |  210

但应该返回

AccountNumber   |  BalanceDate  |  Value
----------------------------------------
1234-0001       |  2017-08-10   |  120
1234-0002       |  2017-08-10   |  210
1234-0003       |  2017-08-09   |  400
1234-0004       |  2017-08-08   |  150

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码

void Main()
{
    List<LedgerAccount> account = new List<UserQuery.LedgerAccount>()
    {
        new LedgerAccount() { AccountNumber = "1234-0001", BalanceDate = new DateTime(2017, 8, 8), Value = 100 },
        new LedgerAccount() { AccountNumber = "1234-0002", BalanceDate = new DateTime(2017, 8, 8), Value = 250 },
        new LedgerAccount() { AccountNumber = "1234-0003", BalanceDate = new DateTime(2017, 8, 8), Value = 500 },
        new LedgerAccount() { AccountNumber = "1234-0004", BalanceDate = new DateTime(2017, 8, 8), Value = 150 },
        new LedgerAccount() { AccountNumber = "1234-0001", BalanceDate = new DateTime(2017, 8, 9), Value = 110 },
        new LedgerAccount() { AccountNumber = "1234-0002", BalanceDate = new DateTime(2017, 8, 9), Value = 230 },
        new LedgerAccount() { AccountNumber = "1234-0003", BalanceDate = new DateTime(2017, 8, 9), Value = 400 },
        new LedgerAccount() { AccountNumber = "1234-0001", BalanceDate = new DateTime(2017, 8, 10), Value = 120 },
        new LedgerAccount() { AccountNumber = "1234-0002", BalanceDate = new DateTime(2017, 8, 10), Value = 210 },
    };

    account.GroupBy(m => m.AccountNumber).Select(m => m.OrderByDescending(s => s.BalanceDate).FirstOrDefault()).Dump();
}

public class LedgerAccount
{
    public string AccountNumber { get; set; }
    public DateTime BalanceDate { get; set; }
    public int Value { get; set; }
}

Linq查询您的要求是

var result = account.GroupBy(m => m.AccountNumber)
        .Select(m => m.OrderByDescending(s => s.BalanceDate).FirstOrDefault())

<强>输出

AccountNumber    BalanceDate               Value
__________________________________________________
1234-0001        8/10/2017 12:00:00 AM       120 
1234-0002        8/10/2017 12:00:00 AM       210 
1234-0003        8/9/2017 12:00:00 AM        400 
1234-0004        8/8/2017 12:00:00 AM        150