Imagine you have a POCO that is back to you. Sort of
public class Item { public int Id { get; set;} public string Name { get; set;} }
Now imagine that you have an IEnumerable<Item> and the collection is full (this is most likely what Dapper returns)
To use the ToDictionary method, you have two important overloads.
var dictionary = itemList.ToDictionary( item => item.Id );
This returns a Dictionary<int,Item> key for the dictionary - this is the property item.Id
Key / Value Overload:
var dictionary = itemList.ToDictionary( item => item.Id , item => item.Name );
This overload creates a Dictionary<int,string> using the specified key item.Id for the key and item.Name for the value.
There are two more overloads that allow you to pass a custom resolver, which will be used when defining keys.
source share