How to populate one extra property in a .Select - LINQ [duplicate]

不想你离开。 提交于 2019-12-13 20:21:42

问题


I'm using my Map method to create DTO object from my context class Company and it looks like this:

private CompDTO Map(Company company)
{
    return new CompDTO()
    {
        Id = company.Id,
        Title = company.Title,
        ParentCompanyId = company.ParentCompanyId,
    };
} 

CompDTO looks like this:

public class CompDTO
{
    public long Id { get; set; }
    public string Title { get; set; }
    public long? ParentCompanyId { get; set; }
    public bool HasChildrens { get; set; }
}

I'm using it like this, basically receiving list of companies and calling another Map method which would create DTO object from my company objects and main issue for me is that Company class does not contain HasChildrens property, so I have to populate it somehow, and I couldn't do it where I'm maping other props because there I dont have access to a companies list.

private IEnumerable<CompDTO> Map(IEnumerable<Company> companies)
{
    // Mapping all properties except HasChildrens because it does not exist in Company object so I decided to map it later

    var result = companies.Select(c => Map(c));

    // Here I wanted to return all previously mapped objects + I would like to add to each object HasChildren property, but obliviously my syntax is not good:

    return result.Select(c => new { c, c.HasChildrens = companies.Any(cc => cc.ParentCompanyId == c.Id) });
}

I'm retrieving error: Invalid anonymous type declarator.

I've tried to add HasChildrens like this also:

return result.Select(c => {c.HasChildrens = companies.Any(cc => cc.ParentCompanyId == c.Id)});

But still issues..

Basically I simply want to add HasChildrens for each my Mapped DTO and return it as it was added in Map method.

Any kind of help would be great!

Thanks


回答1:


The return type of your method private IEnumerable<CompDTO> Map(IEnumerable<Company> companies) is IEnumerable<CompDTO>

So the issue is that you're returning an anonymous type rather than the expected CompDTO

Change return result.Select(c => new { ... }

to

return result.Select(c => new CompDTO { 
    Id = ...
    Title = ...
    ParentCompanyId = ...
    HasChildrens = ...
})

EDIT:

The actual question is:

How do I set the property HasChildrens in the CompDTO while converting from db classes to dto classes

I'd say that the most common way to solve that is to pass in the value while converting:

private CompDTO Map(Company company, bool hasChildrens) {
    return new CompDTO()
    {
        Id = company.Id,
        Title = company.Title,
        ParentCompanyId = company.ParentCompanyId,
        HasChildrens = hasChildrens
    };
}

You could also iterate the result after the fact and set the HasChildrens like so: (I wouldn't recommend this)

foreach(dto in result) {
   dto.HasChildrens = ...
}

You could also insert the logic of obtaining the HasChildrens value inside of the Map method: (I wouldn't recommend this either)

private CompDTO Map(Company company, IEnumerable<Company> companies) {
    return new CompDTO()
    {
        Id = company.Id,
        Title = company.Title,
        ParentCompanyId = company.ParentCompanyId,
        HasChildrens = companies
                       .Any(c => c.ParentCompanyId == company.Id) 
    };
}


来源:https://stackoverflow.com/questions/58256008/how-to-populate-one-extra-property-in-a-select-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!