How to combine elements of a list

Posted by Addie on Stack Overflow See other posts from Stack Overflow or by Addie
Published on 2010-03-08T22:35:45Z Indexed on 2010/03/08 22:51 UTC
Read the original article Hit count: 341

Filed under:
|
|
|

I'm working in c#. I have a sorted List of structures. The structure has a DateTime object which stores month and year and an integer which stores a value. The list is sorted by date. I need to traverse the list and combine it so that I only have one instance of the structure per date.

For example: My initial list would look like this:

{ (Apr10, 3), (Apr10, 2), (Apr10, -3), (May10, 1), (May10, 1), (May10, -3), (Jun10, 3) } 

The resulting list should look like this:

{ (Apr10, 2), (May10, -1), (Jun10, 3) }

I'm looking for a simple / efficient solution.

The struct is:

class CurrentTrade
{
    public DateTime date;
    public int dwBuy;
}

The list is:

private List<CurrentTrade> FillList

© Stack Overflow or respective owner

Related posts about c#

Related posts about list