Is it possible to do a memory efficient Group By for a Sum in Linq?
If I'm using Linq To Objects on a long array of objects from a CSV file like:
Name, Num开发者_开发问答Apples
"Fred", 1
"Fred", 2
"Jane", 2
"Pete", 1
"Fred", 1
"Bob", 3
"Jane", 2
and I'd like to calculate
var query = from p in myfile
group p.NumApples by p.Name into grouped
select new
{
Name = grouped.Key,
TotalApples = grouped.Sum()
};
then my understanding is that the group by operation will create a Dictionary<string, List<int>>
and that the Sum()
values will only be calculated only after the entire file is read.
Is there any way in Linq to avoid the possibly large memory use in Dictionary<string, List<int>>
?
(I realise I can do this by not using Linq, but I love using Linq!)
OK - ignore me....
I should have just bing'ed Jon Skeet instead. Answer is similar to Group and Count
Just by asking the question, I found the answer...
精彩评论