简体   繁体   English

LinQ获取满足条件的最新记录组

[英]LinQ to get the latest group of records satisfying a condition

I have below stated 2 tables: 我在下面列出了2个表格:

在此处输入图片说明

now I want to get the set of Child Table objects for whichever their parent table entries are latest(wr.rt lastmodified). 现在,我想获取其父表条目为最新的子表对象集(wr.rt lastmodified)。 It should be something like.... 它应该像...。

List<Child_Table> List = ChildsList.Where(x=>x.name =="pqr" && status == "done")
    .Select(x=>x.Parent.lastmodified == recent record).....ToList();

You can use GroupBy on the date, then OrderByDescending on the Key then take the First followed by SelectMany to flatten the results. 您可以在日期上使用GroupBy ,然后在Key上使用OrderByDescending ,然后使用“ First和“ SelectMany将结果展平。

var result = ChildsList.Where(x => x.name == "pqr" && x.status == "done")
     .GroupBy(x => x.Parent.lastmodified)
     .OrderByDescending(g => g.Key)
     .First()
     .SelectMany(g => g)
     .ToList();

You could use a join to accomplish it: 您可以使用联接来完成它:

var results = children
    .Join(parents.OrderByDescending(p => p.lastmodified).Take(1),
        c => c.parent_id,
        p => p.id,
        (c, p) => c)
    .Where(x => x.name == "pqr" && x.status == "done")
    .ToList();

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

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