5 Replies - 171 Views - Last Post: 23 June 2020 - 06:09 AM Rate Topic: -----

#1 capsyl   User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 17-June 20

How to print out correlating values from list using min extension?

Posted 23 June 2020 - 04:24 AM

I'm currently using the min extension to get the lowest value from a list in my object. The object has two properties; Temp and Name which is being stored in the list.

What do I write to get the correlating Name for the lowest value of Temp?
float coldestTemp = citiesArray.Min(c => c.Temp);

Is This A Good Question/Topic? 0
  • +

Replies To: How to print out correlating values from list using min extension?

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6901
  • View blog
  • Posts: 28,559
  • Joined: 12-December 12

Re: How to print out correlating values from list using min extension?

Posted 23 June 2020 - 04:29 AM

You could do it in a separate step, now that you have the coldestTemp, using Where and Select to get a list of names.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: How to print out correlating values from list using min extension?

Posted 23 June 2020 - 05:14 AM

var coldestCity = citiesArray
        .OrderBy(c => c.Temp)
        .First();



Sort by temperature (from lowest to highest) and take the first.

Yes, sorting is O(n log n) as compared to the O(n) to find the minimum then another O(n) to find a city that matches that temperature, which in complexity analysis just becomes O(n), but there is a certain elegance to getting the data that you need in just one line. My advice is to do performance testing if this going to be a critical piece of code that is used many times.
Was This Post Helpful? 0
  • +
  • -

#4 capsyl   User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 17-June 20

Re: How to print out correlating values from list using min extension?

Posted 23 June 2020 - 05:54 AM

Thanks for your reply!

Which way would you suggest is the best one? I'm currently learning c#. And only reason why I choose this method was because visual reasons, making it easier to read. At least from a newbies current perspective. If there're better ways which are considered to be more of a best practise I'm eager to know. I understand that it might not be a right or wrong answer depending on performance vs less code to read.
Was This Post Helpful? 0
  • +
  • -

#5 capsyl   User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 17-June 20

Re: How to print out correlating values from list using min extension?

Posted 23 June 2020 - 06:01 AM

If I want to break it up into a string like following. How am I getting each value i.e. Temp and Name?:
Console.WriteLine("Coldest city is {0} at {1} degrees Celsius", x, x)
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6901
  • View blog
  • Posts: 28,559
  • Joined: 12-December 12

Re: How to print out correlating values from list using min extension?

Posted 23 June 2020 - 06:09 AM

Sky's version will get you the first name met having the min value, and is fast. Mine will get you a list of names, as there might be more than one match.

With Sky's, set some variable 'foo' to the expression then foo.Name will get the Name; mine returns a list, so iterate the list to get the name(s).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1