2

Today I'm mapping my objects to DTO like this.

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository.GetArticlesByCategory(catSection, headCategoryID, customerREFID).Select(a => Mapper.ToDTO(a)).ToList();
}

But inside the variable I have another list that I want to map in a similar way. Is it possible to write this all in one line like this or do I have to write an foreach loop then map a.List.

2 Answers 2

1

How about returning the Article and its items in an anonymous object?

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository
        .GetArticlesByCategory(catSection, headCategoryID, customerREFID)
        .Select(a => new 
                     { 
                         Article = Mapper.ToDTO(a),
                         Items = a.Items.Select(b => Mapper.ToDTO(b)).ToList()
                     })
        .ToList();            
}
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry but this is not what i´m after. Maybe i have to do a foreach loop after a got the list of article.
@Tan: What do you want the result to look like? How about you edit the foreach loop version into your question? Then I will be able to help much more easily.
Your method generated an error so i initated a new List of ArticleDTO and did a foreach loop to fill it out with help of your anonumous list. Thanks!
@Tan: Good to hear you got it working! However, I'm considering removing this answer because it is incorrect and it still requires you to use a foreach loop. I'll only remove it if it's okay by you, of course.
0

One way is to use a lambda that has multiple statements. I'm not sure if this can be considered a one liner, and multi-statement lambdas aren't very LINQ-y.

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository
        .GetArticlesByCategory(catSection, headCategoryID, customerREFID)
        .Select(a =>
                {
                    ArticleDTO article = Mapper.ToDTO(a);
                    article.Items = a.Items.Select(b => Mapper.ToDTO(b)).ToList();
                    return article;
                })
        .ToList();
}

If ArticleDTO has a copy constructor you could write it like this:

public IEnumerable<ArticleDTO> GetArticlesBasedOnCategorySection(int catSection, int headCategoryID, string customerREFID)
{
    return _articleRepository
        .GetArticlesByCategory(catSection, headCategoryID, customerREFID)
        .Select(a => new ArticleDTO(Mapper.ToDTO(a))
                     {
                         Items = a.Items.Select(b => Mapper.ToDTO(b)).ToList()
                     })
        .ToList();
}

You could also map the items in a constructor or in Mapper.ToDTO(a).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.