Skip to content
Advertisement

How to populate dictionary using SQL table data

In my C# project, I have a dictionary, its values are currently static.

Now I need to set to its values in dynamically. how can I do that? I have an Oracle DB table called “country” there have two columns called, ID and CountryName as follows,

How can I set this table values to my dictionary? Thanks in advance.

Advertisement

Answer

Here I used ToDictionary extension method, which I used to convert the IEnumerable collection into a new dictionary.

Dotnet fiddle: https://dotnetfiddle.net/KNs9Xl

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement