C# – Convert a dictionary to a list

The simplest way to convert a dictionary to a list is by using the Linq ToList() method, like this: When used on a dictionary, ToList() returns a list of KeyValuePair objects. This example outputs the following: Dictionary keys to list If you want a list of the dictionary’s keys, use ToList() on Dictionary.Keys, like this: … Read more

C# – Check if a value exists in dictionary

Normally you’d check if a dictionary contains a key or get a value by key. But you can also check if the dictionary contains a specific value by using Dictionary.ContainsValue(). Here’s an example: Dictionary.ContainsValue() returns true if the value was found and otherwise returns false. In this example, I initialized the dictionary with a single … Read more

C# – Get value from dictionary

Dictionaries store key/value pairs. When you want to get the value for a key, you can use the indexer syntax and specify the key, like this: Note: In order to show how to get a value, I had to initialize the dictionary with key/value pairs. This returns the value associated with the “Bob” key and … Read more

C# – Map query results to multiple objects with Dapper

When you’re querying joined tables, you can map each row to multiple objects by using the multi mapping feature in Dapper. To multi map, you have to provide Dapper with the following: In this article, I’ll show examples of multi mapping. Note: If you don’t specify the split column, it’ll use the default of “Id”. … Read more