Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:24:19+00:00 2026-05-14T03:24:19+00:00

So I’m trying to learn more about lambda expressions. I read this question on

  • 0

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?? 

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:24:19+00:00Added an answer on May 14, 2026 at 3:24 am

    how do I translate the “var result” of the lambda expression into a usable object that I can then print the result?

    First off, the “lambda expression” is only the portion of the expression that is of the form a=>b. The rest of your query is just a method call that takes a lambda as its argument.

    Anyway, if I could teach people one thing about LINQ it would be this: “result” isn’t the results of the query, it is the query itself.

    If you want to see the results, ask the query for each result:

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

    I would also appreciate an in-depth explanation of what is happening when I declare the outer => outer.Value.Frequency

    Sure. We begin by working out the types of everything involved. We see that the lambda is a function which takes a KeyValuePair and returns an int, so we generate a method

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

    Next we take that method and create a delegate out of it:

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

    and rewrite the extension method call:

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

    and rewrite the var:

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

    I hope you agree that the code you typed in is a whole lot more readable than this mess. Type inference rocks.

    The result is an object which represents the ability to sort this dictionary by the given key. Read that carefully: it represents the ability to sort the dictionary by that key. It does not actually do that until you ask for a result; so far, all it is is an object that says “when asked for a result, sort the dictionary by this key”.

    Suppose you ask for a result. How does it compute the sorted list? It asks the dictionary for each element. Then it calls MyLambda on each element, which gives back an integer, so we now have a pair of dictionary key-value pairs and integers. It then builds a list of pairs sorted on that integer. Then it hands out elements of that list one at a time, as you ask for them.

    We see that the lambda is a function which takes a KeyValuePair and returns an int” – How did you determine that? I don’t see it from the method return value, nor documented in the OrderByDescending().

    Ah, I see the confusion; for pedagogic reasons I fibbed a bit above regarding the exact order in which the semantic analysis proceeds.

    How we do this type inference is one of the more subtle and interesting parts of C#.

    Here’s how it works.

    We see that OrderByDescending is declared as:

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

    and we see we have a potential call to this method:

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

    But we do not know what T and K are. So we start by looking at everything that is NOT a lambda. Your dictionary implements IEnumerable<KeyValuePair<int, FrequencyOrValue>> so we start by saying “T is probably KeyValuePair<int, FrequencyOrValue>“.

    At this point there is nothing else we can deduce from stuff that is not lambdas so we start looking at the lambdas. We see that we have a lambda o=>o.Value.Frequency and so far we have determined that the type of keyExtractor is Func<KeyValuePair<int, FrequencyOrValue>, K> and we are still looking for K. So we say suppose the lambda actually was:

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

    And we ask does it bind? YES! Yes it does. We can successfully compile this lambda without error and when we do so, we see that all of its return statements return an int.

    Therefore we deduce that K is int, and we now have a full type analysis of the whole thing.

    This is a fairly straightforward inference; they can get much weirder. See the “type inference” archive on my blog if this subject particularly interests you.

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

    In particular, here’s a video of me explaining the stuff above plus a few other interesting cases:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This line: GroupFeature GroupFeature=new GroupFeature(); needs to be inside your… May 14, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer A few recommendations: Don't determine your standard exclusively by listening… May 14, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer It's normally a POST, using a form with a special… May 14, 2026 at 8:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.