简体   繁体   中英

LINQ/C# - Making a DTO from a collection?

I'm using EF 6.2 with SQL. Suppose I have these DTO classes:

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 .

ChildModel is from Child , which has a relationship with another entity class SpiritAnimal . Note that I changed it in the .EDMX to Children .

As you can infer, SpiritAnimal has a Description field which I'm trying to retrieve into the ChildModel field, SpiritAnimalDescription .

Naturally, a Parent has a collection of Child , which in turn has one SpiritAnimal (by design). Now, I'm trying to obtain a List<ParentModel> with this code, which currently isn't working:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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