Column value

I followed the code from one of the LINQ lesson as indicated below:

public void Linq56()
{
  var scoreRecords = new [] {
                              new {Name = "Alice", Score = 50},
                              new {Name = "Bob"  , Score = 40},
                              new {Name = "Cathy", Score = 45}
                            };
   
  var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
  Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]);
}

The above code works beautifully and return the result as such: Bob's score: {Name=Bob, Score=40}
I wonder what is the syntax for retrieving only the score instead of the entire row similar to what we fish out the value of a column in a record.

Thank you all for your help.










Answer this question

Column value

  • ssuggs

    Sure.

    Either:
       
      var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
      Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"].Score);
        
    Or:

      var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name, sr => sr.Score);
      Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]);



  • FedericoAX

    Thank you.
  • Column value