[英]Grabbing most recent transaction/record
我在创建一个 linq 语句时遇到问题,该语句将获取在指定开始日期之前发生的最新事务。 想知道是否有人可以帮助我。
例如开始日期是 1 月 20 日。
Id LoanId TransactionDate InterestDate Balance
1 5 January 5 January 3 5000
1 5 January 30 January 5 10000
2 5 January 22 January 22 4000
3 6 January 3 January 1 2000
我应该有一个下面的清单
Id LoanId TransactionDate InterestDate Balance
1 5 January 5 January 3 5000
3 6 January 3 January 1 2000
我在分组和获取正确值时遇到问题。
var transactions = ctx.Transactions.Where(x => x.Date <= startDate)
.GroupBy(x => x.LoanId)
.Select(x => new TransactionDTO
{
LoanId = ...
TransactionDate = ...
InterestDate = ....
Balance = ...
});
一种方法是按startDate
过滤,按 ID 分组,按date
排序,并获取组的第一个元素:
var res = tx
.Where(tx => tx.TransactionDate <= startDate)
.GroupBy(tx => tx.Id)
.Select(g => g.OrderBy(tx => tx.Date).First());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.