简体   繁体   English

LINQ / C#-从集合制作DTO?

[英]LINQ/C# - Making a DTO from a collection?

I'm using EF 6.2 with SQL. 我将EF 6.2与SQL一起使用。 Suppose I have these DTO classes: 假设我有这些DTO类:

private class ParentModel
{
    public string FullName { get; set; }
    public IEnumerable<ChildModel> Children { get; set; }
}
private class ChildModel
{
    public string FullName { get; set; }
    public string SpiritAnimalDescription { get; set; }
}

ParentModel is derived from an entity class Parent . ParentModel派生自实体类Parent

ChildModel is from Child , which has a relationship with another entity class SpiritAnimal . ChildModel来自Child ,它与另一个实体类SpiritAnimal有关系。 Note that I changed it in the .EDMX to Children . 请注意,我在.EDMX中将其更改为Children

As you can infer, SpiritAnimal has a Description field which I'm trying to retrieve into the ChildModel field, SpiritAnimalDescription . 如您SpiritAnimalSpiritAnimal有一个Description字段,我正尝试将其返回到ChildModel字段SpiritAnimalDescription

Naturally, a Parent has a collection of Child , which in turn has one SpiritAnimal (by design). 自然, Parent有一个Child的集合,而Child又有一个SpiritAnimal (通过设计)。 Now, I'm trying to obtain a List<ParentModel> with this code, which currently isn't working: 现在,我正在尝试使用此代码获取List<ParentModel> ,该代码目前无法正常工作:

var query = from p in db.Parents
            join c in db.Children on p.Id equals c.Parent_Id
            join sa in db.SpiritAnimals on c.SpiritAnimal_Id equals sa.Id
            select new ParentModel
            {
                FullName = p.LastName + ", " + p.FirstName
                Children = c.Select(a => new ChildModel // <-- Error here :(
                {
                    FullName = a.FirstName + " " + a.LastName,
                    SpiritAnimalDescription = sa.Description
                }
            };

var list = query.ToList();

How can I solve this, as efficiently as possible? 我如何才能尽可能有效地解决这个问题? Thanks! 谢谢!

EDIT: 编辑:

Entity classes look something like this, for brevity: 为简洁起见,实体类如下所示:

private class Parent
{
    public int Id { get; set; } // PK
    public string LastName { get; set; }
    public string FirstName { get; set; }
}
private class Child
{
    public int Id { get; set; } // PK
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public int Parent_Id { get; set; } // FK
    public int SpiritAnimal_Id { get; set; } // FK
}
private class SpiritAnimal
{
    public int Id { get; set; } // PK
    public string Description { get; set; }
}

Should look something like this: 应该看起来像这样:

var query = from p in db.Parents
        select new ParentModel()
        {
            FullName = p.LastName + ", " + p.FirstName,
            Children = p.Clildren.Select(a => new ChildModel() 
                        {
                            FullName = a.FirstName + " " + a.LastName,
                            SpiritAnimalDescription = sa.Description
                        }).ToList()
        };

Your code cannot be compiled and run, so it is impossible to determine exactly what should be. 您的代码无法编译和运行,因此无法确定确切的内容。

I can only assume that it should be something like this: 我只能假设它应该是这样的:

var query = from p in db.Parents
            select new ParentModel
            {
                FullName = p.LastName + ", " + p.FirstName,
                Children = db.Children.Where(c => c.Parent_Id == p.Id)
                    .Select(c => new ChildModel
                    {
                        FullName = c.FirstName + " " + c.LastName,
                        SpiritAnimalDescription = db.SpiritAnimals
                            .FirstOrDefault(sa => sa.Id == c.SpiritAnimal_Id).Description
                    })
            };

Note: use the navigation properties. 注意:使用导航属性。

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

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