Find a key by value in a dictionary
The Dictionary class represents a collection of keys and values. The
.net framework’s Dictionary is located under the System.Collections.Generic
namespace. We can initialize an empty Dictionary instance and add elements to
it using its Add() method. We also can add some items to the Dictionary at the
initializing time.
The following .net c# tutorial code demonstrates how we can find a key by its value in a dictionary object. We know that the Dictionary element is consist of a key and value pair. The Dictionary keys are unique and we can’t set the same key for multiple items. But the Dictionary values can be duplicated and we can set the same value for the multiple items in a Dictionary.
So, in a Dictionary object, there might be multiple items with the same value. In this situation, we will take only the first element whose value matches our criteria after finding the items.
The Enumerable First() method returns the first element of a sequence. And the FirstOrDefault() method returns the first element of a sequence, or a default value if no element is found. So, we can find the first key by value in a Dictionary. We can easily pass the condition to the First() and FirstOrDefault() methods. The condition is matching the value. Then we can get the key from the returned element.
The following .net c# tutorial code demonstrates how we can find a key by its value in a dictionary object. We know that the Dictionary element is consist of a key and value pair. The Dictionary keys are unique and we can’t set the same key for multiple items. But the Dictionary values can be duplicated and we can set the same value for the multiple items in a Dictionary.
So, in a Dictionary object, there might be multiple items with the same value. In this situation, we will take only the first element whose value matches our criteria after finding the items.
The Enumerable First() method returns the first element of a sequence. And the FirstOrDefault() method returns the first element of a sequence, or a default value if no element is found. So, we can find the first key by value in a Dictionary. We can easily pass the condition to the First() and FirstOrDefault() methods. The condition is matching the value. Then we can get the key from the returned element.
dictionary-find-key-by-value.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//initialize a dictionary with keys and values.
Dictionary<int, string> birds = new Dictionary<int, string>() {
{10,"Sooty Tern"},
{20,"Herring Gull"},
{30,"Kelp Gull"},
{40,"Black Skimmer"},
{50,"South Polar Skua"}
};
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
//get key of value 'Sooty Tern' from dictionary.
int keyOfSootyTern = birds.FirstOrDefault(x => x.Value == "Sooty Tern").Key;
//find key of value 'Kelp Gull' from dictionary.
int keyOfKelpGull = birds.FirstOrDefault(x => x.Value == "Kelp Gull").Key;
Label1.Text += "<br /><br />key of value Sooty Tern: " + keyOfSootyTern;
Label1.Text += "<br />key of value Kelp Gull: " + keyOfKelpGull;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary find key by value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary find key by value
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="dictionary find key by value"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>