Convert query result to Dictionary in CSharp
Description
The following code shows how to convert query result to Dictionary.
Example
/*from ww w . j a va2s. c om*/
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["B"]);
Console.WriteLine("Bob's score: {0}", scoreRecordsDict["B"].Name);
Console.WriteLine("Bob's score: {0}", scoreRecordsDict["B"].Score);
}
}
The code above generates the following result.