question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Map a List to a Grouped DTO

See original GitHub issue

I have a list of entities and I need to map it to a DTO, but grouped.

My entity Class:

Class myEntity
{
      public int id {get;set;}
      public double cost {get;set;}
}

My DTO Class

Class myEntityDTO
{
      public int id {get;set;}
      public double cost {get;set;}
}

My mapping:

CreateMap<myEntity, myEntityDTO>()
                //register all properties mapped
                ;
var entities = new List<myEntity>();
// fill the list
//  [
//          {1,50},
//          {1,30},
//          {1,25},
//          {2,80},
//          {2,20},
//          {2,45},
//          {2,38},
//          {3,10},
//          {3,15}
//   ]
Mapper.Map<List<myEntityDTO>>(entities);

I need to group my list of myEntity (by id for example, and sum cost), so, my DTO should be like this: [ {id: 1, cost: 105}, {id: 2, cost: 183}, {id: 3, cost: 25} ]

I hope I made myself clear, my english is not good.

Thanks in advance. Regards!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
lbargaoanucommented, May 14, 2017

Even for mapping the grouping to the DTO, I still think AM wouldn’t be that useful because then you would have to customize the default map and it’s not auto anymore, is it? Just LINQ seems the best.

0reactions
lock[bot]commented, May 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Group List to List DTO using Java 8
I would like to group a list of InscricaoTipoTrabalhoAvaliador in a DTO by equal AvaliadorEvento using JAVA 8. Could someone help? Bank query ......
Read more >
Java Streams: The Dynamic Grouping Solution You've ...
Grouping data is a common task when working with streams in Java. It allows us to group ... Step 1 :Creating a map...
Read more >
How to Convert List to Map in Java
Learn about different ways of converting a List to a Map in Java, using core functionalities and some popular libraries.
Read more >
10 Examples of Converting a List to Map in Java 8
groupingBy() method to group elements in Set and In this article, we will use Collectors.toMap() method to convert a List of an object...
Read more >
groupBy - Kotlin Programming Language
Groups elements of the original array by the key returned by the given ... a map where each group key is associated with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found