ToDictionary: convert query result to Dictionary

   
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        var scoreRecords = new[] { new {Name = "A", Score = 50},
                                new {Name = "B" , Score = 40},
                                new {Name = "C", Score = 45}
                              };
        var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
        Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]);
    }
}

    



This entry was posted in LINQ. Bookmark the permalink.