Answered by:
Strange behaviour for Linq Aggregate in C#
Question

In the above code, Aggreg.Sum_Of_Squares is not correct. Please let me know why?
Double[] values = new Double[] { 2.2, 2.3, 3.3, 4.1, 5.6 }; var Aggreg = new { Count = (from v in values select v).Count(), Sum = (from v in values select v).Aggregate((cnt, v) => cnt += v), Sum_Of_Squares = (from v in values select v).Aggregate((cnt, v) => cnt +=+ Math.Pow(v,2)) }; Console.WriteLine(Aggreg.Count); Console.WriteLine(Aggreg.Sum); Console.WriteLine(Aggreg.Sum_Of_Squares);
Please mark the post answered if it serves your query.
Subhendu De
Answers

Hello subgendude,
Welcome back. This is Jackie, you don’t know why it can’t show the correct answer.
During I solve this issue, I found that is the difference between Aggregate and Sum Functions.
The Aggregate function differs from numeric aggregate functions like Sum in that the Aggregate function returns a value that is calculated by the data provider or data processing extension. Numeric aggregate functions like Sum return a value that is calculated by the report processor on a set of data from the dataset that is determined by the scope parameter. For more information, see in the aggregate functions listed in Using Builtin Report and Aggregate Functions in Expressions.
Based on your code, if you want to get sum of squares, you should use the Sum function, like this.
Sum_Of_Squares = (from v in values
select v).Sum((v) => Math.Pow(v,2))
I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.
Thank you for your understanding and support.
I look forward to hearing from you.
Best Regards,
Jackie Sun [MSFT]
MSDN Community Support  Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
 Marked as answer by DE, SubhenduMicrosoft employee Monday, December 13, 2010 3:53 PM
All replies

Hello subgendude,
Welcome back. This is Jackie, you don’t know why it can’t show the correct answer.
During I solve this issue, I found that is the difference between Aggregate and Sum Functions.
The Aggregate function differs from numeric aggregate functions like Sum in that the Aggregate function returns a value that is calculated by the data provider or data processing extension. Numeric aggregate functions like Sum return a value that is calculated by the report processor on a set of data from the dataset that is determined by the scope parameter. For more information, see in the aggregate functions listed in Using Builtin Report and Aggregate Functions in Expressions.
Based on your code, if you want to get sum of squares, you should use the Sum function, like this.
Sum_Of_Squares = (from v in values
select v).Sum((v) => Math.Pow(v,2))
I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.
Thank you for your understanding and support.
I look forward to hearing from you.
Best Regards,
Jackie Sun [MSFT]
MSDN Community Support  Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
 Marked as answer by DE, SubhenduMicrosoft employee Monday, December 13, 2010 3:53 PM

Hello Jackie...
Your solution rocks!!!
Thanks Buddy
Please mark the post answered if it serves your query.
Subhendu De 
You are welcome.
It's my pleasure to help you. If that helps you, please remember to mark it as answer.
Thanks a lot!
Best Regards,
Jackie Sun [MSFT]
MSDN Community Support  Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

just use the prototype of the function will get correct result
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
var Aggreg = new
{
Count = (from v in values
select v).Count(),
Sum = (from v in values
select v).Aggregate((cnt, v) => cnt += v),
Sum_Of_Squares = (from v in values
select v).Aggregate(0.0,(cnt, v) => cnt +=+ Math.Pow(v,2) ,r=>r*1.0)
};