Dictionary remove by value
dictionary-remove-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>() {
{1,"Indian Vulture"},
{2,"Barred Fruiteater"},
{3,"Indian Black Eagle"},
{4,"Indian Pitta"},
{5,"Acadian Flycatcher"}
};
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
//get keys which value starts with [Indian]
int[] keysToDelete = birds.Where(x => x.Value.StartsWith("Indian")).Select(x => x.Key).ToArray();
Label1.Text += "<br /><br />removing elements from dictionary which value starts with [Indian]<br />";
foreach (int key in keysToDelete)
{
birds.Remove(key);
Label1.Text += "key [" + key + "] removed from dictionary<br />";
}
Label1.Text += "<br />elemetns removed which value starts with [Indian]..........";
Label1.Text += "<br />dictionary elements after remove..........";
foreach (KeyValuePair<int, string> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary remove by value</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary remove 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 remove by value"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to remove range of items from a Dictionary
- How to get a range of items from a Dictionary
- How to remove items from a Dictionary by condition
- How to convert Dictionary keys to list
- How to access Dictionary items by index
- How to get index of an item by key from a Dictionary
- How to perform foreach loop through Dictionary keys
- How to get a value by key from a Dictionary
- How to find a key by value in a Dictionary
- How to convert a Dictionary to a string