Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add to a list using Linq's aggregate function C#

Tags:

c#

linq

aggregate

I have a collection of objects of one type that I'd like to convert to a different type. This can be done easily with foreach, but I'd like to figure out how to use Linq's aggregate function to do it.

The problem is all the Aggregate examples use types line string or int, which support the '+' operator. I'd like to have the accumulator type be a list, which doesn't support '+' semantics.

Here's a quick example:

public class DestinationType
{
    public DestinationType(int A, int B, int C) { ... }
}

var set = from item in context.Items
          select new { item.A, item.B, item.C };

var newSet = set.Aggregate( new List<DestinationType>(),
                            (list, item) => list.Add(new DestinationType(item.A, item.B, item.C)) );

The problem is that List<>.Add returns void. The return type of the second parameter to Aggregate needs to be a List.

If I had a list type that supported '+' type semantics I could just make the second parameter

list + item

However I can't find any collection type that supports this kind of thing.

Seems like this should be easily possible in Linq. Is there a way? Also, if I'm missing an entirely easier way, I'd love to learn about that too. Thanks!

like image 944
Slaggg Avatar asked Jun 13 '09 16:06

Slaggg


3 Answers

Assuming this is LINQ to Objects, try...

var newSet = set.Aggregate(new List<DestinationType>(),
                                    (list, item) =>
                                    {
                                        list.Add(new DestinationType(item.A, item.B, item.C));
                                        return list;
                                    });
like image 145
Dustin Campbell Avatar answered Oct 21 '22 10:10

Dustin Campbell


I think a call to Select combined with ToList() might be what you need here. For example:

context.Items
  .Select(item => new DestinationType(item.A, item.B, item.C))
  .ToList();
like image 27
Samuel Jack Avatar answered Oct 21 '22 10:10

Samuel Jack


You can just apply select here.

var newSet = set.Select(item => new DestinationType(...)).ToList();

Aggregate (generally known as fold or reduce) is used to combine elements together where select applies a function to each element.

Ex:

Let f be a unary function, then [a, b, c].select(f) equals [f(a), f(b), f(c)].

Let f be a binary function, then [a, b, c].aggregate(f, init) equals f(a, f(b, f(c, init))).

The way you chose in your example is uncommon in C# but often used in functional programming where (linked) lists are transformed into new lists instead of changing an existing collection:

reversed = fold (\list element -> element:list) [] [1..10]

If you really want to do this computation with aggregate, use Dustin's solution or better implement a linked-list-based type for immutable collections (you can even give this type an operator +).

like image 4
Dario Avatar answered Oct 21 '22 08:10

Dario