繁体   English   中英

获取最近的交易/记录

[英]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());

排序列表<object>通过抓取具有最近日期的重复项<div id="text_translate"><p>我有一个包含(ID,日期)的对象列表。 在某些情况下,IDS 会重复,但日期不同。 这两个都是字符串。</p><p> 我需要通过获取 ID 和最近日期对列表进行排序,同时删除较旧的日期。</p><p> 例子</p><pre>ID: 1 Date 1-1-2018 ID: 1 Date: 1-1-2019</pre><p> 我试过使用下面的代码。</p><pre> unsortedList.GroupBy(x =&gt; x.Id).Select(group =&gt; group.OrderByDescending(record =&gt; Convert.ToDateTime(record.Date)));</pre><p> 我仍然看到原始形式的 ID。</p><pre> public class Record { public string Id { get; set; } public string Date { get; set; } }</pre></div></object>

[英]Sort List<Object> with Duplicates by grabbing the one with the most recent date

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM