Linq group by parent property by child

Kev*_*vin 8 c# linq

基本上我想要做的是让父母按Parent.type分组,其中Child.type的状态为'y',由child.Date排序

作为一个例子,我创建了这些类:

public class Collection
{
    public IEnumerable<Parent> Parents { get; set; }
    public int Id { get; set; }
}

public class Parent
{
    public int Id { get; set; }
    public Type type { get; set; }       
    public IEnumerable<Child> Children { get; set; }
}

public class Child
{
    public int Id { get; set; }
    public Status Status { get; set; }
    public DateTime Date { get; set; }
}

public enum Type
{
    a,
    b
}

public enum Status
{
    x,
    y
}
Run Code Online (Sandbox Code Playgroud)

我想要的是每个类型的父母列表,其中儿童的日期值最高.

为了得到这个例子,我添加了一些测试数据:

Collection collection= new Collection
        {
            Id = 1,
            Parents = new List<Parent>
            {
                new Parent
                {
                    Id= 1,
                    type = Type.a,
                    Children = new List<Child>
                    {
                        new Child
                        {
                            Id = 1,
                            Status = Status.y,
                            Date = DateTime.Now.AddDays(-1)
                        },
                        new Child
                        {
                            Id = 2,
                            Status = Status.x,
                            Date = DateTime.Now.AddDays(-1)
                        }
                    }
                },
                new Parent
                {
                    Id= 2,
                    type = Type.b,
                    Children = new List<Child>
                    {
                        new Child
                        {
                            Id = 3,
                            Status = Status.y,
                            Date = DateTime.Now.AddDays(-2)
                        },
                        new Child
                        {
                            Id = 4,
                            Status = Status.x,
                            Date = DateTime.Now.AddDays(-2)
                        }
                    }
                },
                new Parent
                {
                    Id= 3,
                    type = Type.a,
                    Children = new List<Child>
                    {
                        new Child
                        {
                            Id = 5,
                            Status = Status.y,
                            Date = DateTime.Now.AddDays(-3)
                        }
                    }
                },
                new Parent
                {
                    Id= 4,
                    type = Type.b,
                    Children = new List<Child>
                    {
                        new Child
                        {
                            Id = 6,
                            Status = Status.y,
                            Date = DateTime.Now.AddDays(-3)
                        },
                        new Child
                        {
                            Id = 7,
                            Status = Status.y,
                            Date = DateTime.Now.AddDays(-4)
                        },
                        new Child
                        {
                            Id = 8,
                            Status = Status.x,
                            Date = DateTime.Now.AddDays(-4)
                        }
                    }
                },
            }
        };
Run Code Online (Sandbox Code Playgroud)

select的结果应该是包含2个Parent的List.每种类型一个(a和b).这两个仍包含所有Child对象.

这有什么简单的解决方案吗?

我尝试过类似的东西:

List<Parent> test = collection.Parents
                .GroupBy(m => m.type)
                .Select(
                m => m.OrderByDescending(
                    mm => mm.Children.Select(
                        r => r).OrderByDescending(
                            r => r.Date)).FirstOrDefault()).ToList();
Run Code Online (Sandbox Code Playgroud)

但这并没有结束.

=====更新=====

感谢Enigmativity的例子,这个问题已经解决了.我对linq查询稍作修改,因为我使用的原始内容是由Entity Framework(6)提供的.这对我来说意味着所选的Parent对象不包含任何Children,所以我必须选择一个新的对象类型(因为我们无法实例化Entity Framework模型提供的类型的新对象).我的父母也在另一个IEnumerable中,所以我不得不在Parent上使用SelectMany.

这导致了这样的事情:(虽然这不适用于测试类)

var result =
    collection
        .SelectMany(c => c.Parents)
        .GroupBy(p => p.type)
        .SelectMany(gps =>
            gps
                .SelectMany(
                    gp => gp.Children.Where(c => c.Status == Status.y),
                    (gp, c) => new { gp, c })
                .OrderByDescending(gpc => gpc.c.Date)
                .Take(1)
                .Select(gpc => new ParentData {Id = gpc.gp.Id, Children= gpc.gp.Children}))
        .ToList();
Run Code Online (Sandbox Code Playgroud)

Eni*_*ity 2

如果我正确理解了您的要求,您希望按类型对所有父母进行分组,然后根据该父母的孩子与该组中所有孩子中日期最高的孩子,从每个组中仅选择一名父母。

这是我想出的:

var result =
    collection
        .Parents
        .GroupBy(p => p.type)
        .SelectMany(gps =>
            gps
                .SelectMany(
                    gp => gp.Children.Where(c => c.Status == Status.y),
                    (gp, c) => new { gp, c })
                .OrderByDescending(gpc => gpc.c.Date)
                .Take(1)
                .Select(gpc => gpc.gp))
        .ToList();
Run Code Online (Sandbox Code Playgroud)

这给了我:

结果