How to Get the Dictionary Key by value in C# ?

Published on February 7, 2016 by abundantcode

Filed under Collections

Last modified February 6, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 2120 times

If you want to get the Key of a dictionary based on the value in C# , you can use the LINQ’s where clause o FirstOrDefault method.

How to Get the Dictionary Key by value in C# ?

using System;
using System.Collections.Generic;
using System.Linq;

namespace ACCode
    class Program
        static void Main(string[] args)
            Dictionary<string, string> dictionaryItems= new Dictionary<string, string>()
                {"1", "One"},
                {"2", "Two"},
                {"3", "Three"},
                {"4", "Three"},
                {"5", "Three"}
            var dictValue = dictionaryItems.FirstOrDefault(x => x.Value == "Three").Key;

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

How to Get the number of elements in ArrayList in C# ?

Here's the code that demonstrates how one can get the total number of elements contained in the Arra...

How to Check if the ArrayList is read-only in C# ?

There are times when you would use the read-only wrapper in order to prevent modifying the ArrayList...

How to get the CommandLine of the Current Application in C# ?

You might need to get the information that contains the commandline which is used to execute the cur...