How to correctly translate the result of the "var" lambda expression to a specific type?

So I'm trying to learn more about lambda expressions. I read this question on stackoverflow, agree with the selected answer and tried to implement the algorithm using a console application in C # using a simple LINQ expression.

My question is: how do I translate the "result of var" of a lambda expression into a useful object that I can then print the result of?

I would also like to receive a detailed explanation of what happens when I announce outer => outer.Value.Frequency

(I read numerous explanations of lambda expressions, but an 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?? 

For an answer complete with print commands, see my answer here .

+3
4

" var" , ?

-, "-" - , a=>b. - , .

, LINQ, : "" - , .

, :

foreach(var item in result)
    Console.WriteLine(item.ToString());

, , = > external.Value.Frequency

. . , - , KeyValuePair int,

static private int MyLambda(KeyValuePair<int, FrequencyAndValue> outer)
{
    return outer.Value.Frequency;
}

:

var result = dictionary.OrderByDescending(
    new Func<KeyValuePair<int, FrequencyAndValue>, int>(MyLambda));

:

var result = Enumerable.OrderByDescending<KeyValuePair<int, FrequencyAndValue>, int>(
    dictionary,
    new Func<KeyValuePair<int, FrequencyAndValue>, int>(MyLambda));

var:

IOrderedEnumerable<KeyValuePair<int, FrequencyAndValue>> result =
    Enumerable.OrderByDescending<KeyValuePair<int, FrequencyAndValue>, int>(
    dictionary,
    new Func<KeyValuePair<int, FrequencyAndValue>, int>(MyLambda));

, , , . .

, . : . , ; , " , ".

, . ? . MyLambda , , . , . , .

, - , KeyValuePair int ". ? OrderByDescending().

, ; , .

, #.

.

, OrderByDescending :

static IOrderedEnumerable<T> OrderByDescending<T, K>(
    this IEnumerable<T> sequence, 
    Func<T, K> keyExtractor)

, :

OrderByDescending(dictionary, o=>o.Value.Frequency)

, T K. , . IEnumerable<KeyValuePair<int, FrequencyOrValue>>, , "T, , KeyValuePair<int, FrequencyOrValue>".

, , , . , lambda o=>o.Value.Frequency, , keyExtractor Func<KeyValuePair<int, FrequencyOrValue>, K>, K. , , , :

(KeyValuePair<int, FrequencyOrValue> o)=>{return o.Value.Frequency;}

, ? ! . , , , return int.

, K int, .

; . . "" , .

http://blogs.msdn.com/ericlippert/archive/tags/Type+Inference/default.aspx

, , , :

http://blogs.msdn.com/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx

+27

OrderByDescending IEnumerable, IOrderedEnumerable, TSource .

, OrderByDescending a:

 IOrderedEnumerable<KeyValuePair<int, FrequencyAndValue>>

.

+2
var result = dictionary.OrderByDescending(outer => outer.Value.Frequency);

IOrderedEnumerable<KeyValuePair<int, FrequencyAndValue>>. ,

Func<KeyValuePair<int,FrequencyAndValue>, int>

, KeyValuePair<int, FrequencyAndValue> ( ) , . , IOrderedEnumerable .

+1

, "", IOrderedEnumerable.

C#
static void Main(string[] args)
    {

      //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);

      // BEGIN Priting results with the help of stackoverflow answers 
      Console.Write("With Items: ");    
      foreach (var item in result)
      {
        for (int i = 0; i < item.Value.Frequency; i++)
        {
          Console.Write(item.Value.Value + " ");
        }
        //Console.WriteLine(item.Value.Frequency + " " + item.Value.Value);
      }
      Console.WriteLine();

      Console.Write("With IOrderedEnumerable: ");    
      IOrderedEnumerable<KeyValuePair<int, FrequencyAndValue>> myres = result;
      foreach (KeyValuePair<int, FrequencyAndValue> fv in myres)
      {
        for(int i = 0; i < fv.Value.Frequency; i++ )
        {
          Console.Write(fv.Value.Value + " ");
        }
      }
      Console.WriteLine();
      // END Priting results with the help of stackoverflow answers 
      Console.ReadLine();
    }
    class FrequencyAndValue
    {
      public int Frequency{ get; set;}
      public int Value{ get; set;}
      public FrequencyAndValue(int myFreq, int myValue)
      {
        Value = myValue;
        Frequency = myFreq;
      }
    }
}
0

All Articles