This article will show you how you perform c# dictionary
search key by value. In this we will pass the string value and get the key
integer value.
Some of my previous articles are as follows: How
to Extract Text From PDF File Using C#.Net, How
to Read a Text File Line by Line in C# in Console Application, Retrieve
All Files Present inside a Directory & Sub Directory Using C#.Net,
C#
Dictionary Get Value By Key.
So for this article we will create a new console application
and add the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static
void Main(string[]
args)
{
Console.WriteLine("Search For: America value\n");
Dictionary<string, int>
dictionary = new Dictionary<string, int>();
dictionary.Add("India", 1);
dictionary.Add("Pakistan", 2);
dictionary.Add("Sri Lanka", 3);
dictionary.Add("America", 4);
var
Value = (from data in
dictionary
where data.Key == "America"
select data.Value).FirstOrDefault();
if
(dictionary.ContainsKey("Pakistan"))
{
//int
value = dictionary["Pakistan"];
Console.WriteLine("Pakistan key is : " + Value);
}
Console.WriteLine("\n\n------End Search--------\n");
Console.ReadLine();
}
}
}
|
0 comments:
Please let me know your view