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; Console.WriteLine(dictValue); Console.ReadLine(); } } }
Leave a Reply