ToDictionary

Query
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"]);
}
Lambda Expression
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"]);
}
Output
Bob's score: { Name = Bob, Score = 40 }
Related Tutorial
1. ToArray
2. ToList
4. OfType
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 # # # # #