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!