如果存在自引用层次结构时的记录,我想获取每个组的最新记录。
我的表格是这样的:
RecordId CreatedDate ParentRecordId
1 2012/05/13 NULL
2 2012/05/13 NULL
3 2012/05/14 1
4 2012/05/15 3
我只想选择最新版本的记录。
所以在这种情况下我只想选择 RecordId = 2 和 RecordId =4。
这是我目前所拥有的,但我被卡住了。
db.Records
.Where(x => x.ParentRecordId!= null).GroupBy(x => x.ParentRecordId)
.SelectMany(x=>x.OrderByDescending(y=>y.CreatedDate).Take(1)).ToList();
最佳答案
我的左连接有点欠缺,但像这样应该可以做到;
var query = from r1 in db.Records
join r2 in db.Records
on r1.RecordId equals r2.ParentRecordId into rec
from r in rec.DefaultIfEmpty()
where r == null
select r1;
关于c# - Linq 选择最新的组层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16488479/