How to properly translate the "var" result of a lambda expression to a concrete type?
Posted
by CrimsonX
on Stack Overflow
See other posts from Stack Overflow
or by CrimsonX
Published on 2010-03-26T14:16:39Z
Indexed on
2010/03/26
14:23 UTC
Read the original article
Hit count: 270
So I'm trying to learn more about lambda expressions. I read this question on stackoverflow, concurred with the chosen answer, and have attempted to implement the algorithm using a console app in C# using a simple LINQ expression.
My question is: how do I translate the "var result" of the lambda expression into a usable object that I can then print the result?
I would also appreciate an in-depth explanation of what is happening when I declare the outer => outer.Value.Frequency
(I've read numerous explanations of lambda expressions but additional clarification would help)
C#
//Input : {5, 13, 6, 5, 13, 7, 8, 6, 5}
//Output : {5, 5, 5, 13, 13, 6, 6, 7, 8}
//The question is to arrange the numbers in the array in decreasing order of their frequency, preserving the order of their occurrence.
//If there is a tie, like in this example between 13 and 6, then the number occurring first in the input array would come first in the output array.
List<int> input = new List<int>();
input.Add(5);
input.Add(13);
input.Add(6);
input.Add(5);
input.Add(13);
input.Add(7);
input.Add(8);
input.Add(6);
input.Add(5);
Dictionary<int, FrequencyAndValue> dictionary = new Dictionary<int, FrequencyAndValue>();
foreach (int number in input)
{
if (!dictionary.ContainsKey(number))
{
dictionary.Add(number, new FrequencyAndValue(1, number) );
}
else
{
dictionary[number].Frequency++;
}
}
var result = dictionary.OrderByDescending(outer => outer.Value.Frequency);
// How to translate the result into something I can print??
© Stack Overflow or respective owner