C# getKey from a IDictionary by Value - Values can be repeated - c#

I have this public static IDictionary<int, int> dictionaryLIndex;
that after filling it and printing it gives me this:
key = 0, value = 18
key = 1, value = 2
key = 2, value = 1
key = 3, value = 18
key = 4, value = 3
This Dictionary is being created by using:
dictionaryLIndex= ELWIndex.ToDictionary(pair => pair.Key, pair => pair.Value);
ELWIndex is a IList<KeyValuePair<int,int>> So the Key i print from the dictionary is actually the index of a list.
Clarifying that, I need to get the key of the dict based on a value. The thing here is that this values can be repeted so how can i accomplish this? Taking in count that this proccess can be called from time to time.
The final goal is to get that key (index from a list) and search in another list for that position and do something to that index.
Just one thought that just came to me. After getting the first value with that key, can i delete that key-value pair from the dict all together?? So the next time there will be just one key with that value...
So how can i get the key from a value given? and, is this last second thought viable?

Use FirstOrDefault to get the first key-value pair from a dictionary based on a value:
var search = 18;
var kvp = dictionaryLIndex
.Where(p => p.Value == search)
.Cast<KeyValuePair<int,int>?>()
.FirstOrDefault();
If kvp is null, search value is not in the dictionary.
If kvp is not null, use kvp.Key to find the key. You can later delete that key from dictionaryLIndex.
if (kvp.HasValue) {
var key = kvp.Value.Key;
Console.WriteLine("Key = {0}", key);
}
Note: A cast to nullable KeyValuePair<int,int>? is necessary because KeyValuePair<int,int> is a value type, and both its members are value types as well.

Assuming that I understood you correctly and you are trying to create another dictionary that does not contain duplicate values, I think that this could work for you:
Dictionary<int, int> noDuplicates = new Dictionary<int, int>();
foreach (var item in dictionaryLIndex)
{
if (!noDuplicates.ContainsValue(item.Value))
{
noDuplicates.Add(item.Key,item.Value);
}
}

Related

C#, Linq Search Dictionary in C# and return string using key

I have dictionary created as
Dictionary<string, string> delta = new dictionary<string, string>();
delta.Add("A", "One");
delta.Add("B", "Two");
delta.Add("C", "Three");
I wanted to retrieve value based on the value passed as key
public string GetValuefromdictionary(string roll. Dictionary<string, string> delta)
{
string rollValue;
return rollValue = delta
.Where(d => d.Key.Contains(roll))
.Select(d => d.Value)
.ToString();
}
however I am seeing it doesn't return me the string and I get like this
System.Linq.Enumerable+WhereSelectEnumerableIterator``2[System.Collections.Generic.KeyValuePair``2[System.String,System.String],System.String]
any help
if a Key is "ABC", and you pass "A" as roll, and you want to return the value from "ABC" because "ABC" contains "A". You can do this:
return delta.FirstOrDefault(d=> d.Key.Contains(roll)).Value;
Two options, better I would say two cases.
Case 1
Let's say Dictionary Values are unique, in this case we can simply transpose the dictionary swapping <Key,Value> pair.
var transformDictionary = delta.ToDictionary(kp => kp.Value, kp => kp.Key);
Now we can access this like any other dictioanry.
transformDictionary["One"];
Case 2 :
Values are not unique, in this case use LookUp.
var lookup = delta.ToLookup(c=>c.Value, c=>c.Key);
var lookupvalue = ((IEnumerable<string>)lookup["One"]).First();
Working Demo
You want to search for an element in a Dictionary, but using value instead of > a key. So you iterate through you dictionary and return the
first element that has the value you are looking for. The output you
get is a keyValue pair, hence the .Key
string key = "One";
string value = delta.FirstOrDefault(d => d.Value.Contains(key)).Key;

How to get value from dictionary without knowing key?

I want to get value from dictionary but i don't know key(Because dynamic generate dictionary from database) how can i get dictionary value.
If you some idea share me ...
For Example my database string value like
string jsonString = " "FB": "[{\"title\":\"sheet1\",\"rows\":[{\"height\":\"undefined\",\"columns\":[{\"value\":\"Cover Group \"},{\"value\":\"Sample Variable\"},{\"value\":\"Coverpoint Name\"},{\"value\":\"Crossed cover points\"},{\"value\":\"Coverpoint Comment\"},{\"value\":\"Bin Type\"},{\"value\":\"Bin Id\"},{\"value\":\"Sample Value\"},{\"value\":\"Expected Bin Count\"},{\"value\":\"Set Max Bin\"},{\"value\":\"Not Used\"}]},{\"height\":\"undefined\",\"columns\":[{\"value\":\"allCg,allSi\"},{\"value\":\"exSingle\"},{\"value\":\"exSingle\"},{},{\"value\":\"Example for single bin\"},{\"value\":\"single\"},{\"value\":\"valZero\"},{\"value\":\"1'b0\"},{\"formula\":\"1\",\"value\":1},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{\"value\":\"single\"},{\"value\":\"valOne\"},{\"value\":\"1'b1\"},{\"formula\":\"1\",\"value\":1},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex1Bus[3:0]\"},{\"value\":\"exMulti\"},{},{\"value\":\"Example for multibin\"},{\"value\":\"multi\"},{},{\"value\":\"[0:15]\"},{\"formula\":\"16\",\"value\":16},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{\"value\":\"exCross\"},{\"value\":\"exSingle,exMulti\"},{\"value\":\"Example for cross\"},{\"value\":\"Implicit\"},{},{},{\"formula\":\"32\",\"value\":32},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex2Bus[15:0]\"},{\"value\":\"exWildcard\"},{},{\"value\":\"example for wildcard\"},{\"value\":\"wildcard\"},{\"value\":\"ex_wildcard\"},{\"value\":\"16'bxxxxxxxxxxxxxx1\"},{\"formula\":\"1\",\"value\":1},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex3Bus[4:0]\"},{\"value\":\"exImplicit\"},{},{\"value\":\"example for implicit & set max bin\"},{\"value\":\"Implicit\"},{},{},{\"formula\":\"8\",\"value\":8},{\"formula\":\"8\",\"value\":8},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex4Bus[3:0]\"},{\"value\":\"ex4Bus\"},{},{\"value\":\"setup for ignore example\"},{\"value\":\"multi\"},{},{\"value\":\"[0:15]\"},{\"formula\":\"16\",\"value\":16},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{\"value\":\"exIgnore\"},{\"value\":\"exSingle,ex4Bus\"},{\"value\":\"example for ignore\"},{\"value\":\"ignore\"},{},{\"value\":\"ex4Bus([12:15])\"},{\"formula\":\"24\",\"value\":24},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex5Bus[3:0]\"},{\"value\":\"exIllegal\"},{},{\"value\":\"example for illegal\"},{\"value\":\"illegal\"},{},{\"value\":\"[12:15]\"},{\"formula\":\"16\",\"value\":16},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]}],\"metadata\":{\"widths\":[\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\"],\"frozenAt\":{\"row\":0,\"col\":0}}}]""
FB is dynamic key and after it's value title all value i need
If you don't have the key, but have the value and trying to get hold of the key, you can do this:
Dictionary<string, string> testData = new Dictionary<string, string>();
testData.Add("name", "Latheesan");
testData.Add("age", "26");
KeyValuePair<string, string> searchResult
= testData.FirstOrDefault(s => s.Value == "Latheesan");
string key = searchResult.Key; // returns "name" here
To get a sequence of all the Key/Value pairs where the value matches a target:
var dict = new Dictionary<string, int>
{
{"One", 1},
{"Two", 2},
{"Another One", 1},
{"Three", 3},
{"Yet Another One", 1}
};
int target = 1; // For example.
var matches = dict.Where(item => item.Value == target);
foreach (var kvp in matches)
Console.WriteLine("Key = " + kvp.Key);
The sample data you posted isn't a flat key-value dictionary. It contains embedded dictionaries - the base dictionary contains a title and a rows, which in turn consists of height and columns and so on, and at some point are key-value pairs who keys are, confusingly, named 'value'. Are you asking how to parse this data structure to get all the values whose key is value?
What you first need to do, since this appears to be a JSON-formatted entry, is parse the JSON into a .NET data structure, using libraries like JSON.NET or System.Web.Helpers.Json. These libraries will convert the JSON string into a hierarchy of dictionaries, all of them implementing IEnumerable, so you can iterate over it, more or less like this (this is not compilable code, just a demonstration!):
public void Main()
{
var jsonObject = Json.Decode(FB); // FB is your JSON string.
var values = new List<string>();
FindValues(jsonObject);
}
public void FindValues(jsonObject, values)
{
foreach (var child in jsonObject)
{
if (child.key == 'value')
{
values.Add(child.value);
}
// Recursively call FindValues on child objects.
FindValues(child, values);
}
}
This C#-ish pseudo-code shows you how to go over a dictionary, then optionally drill down deeper into internal dictionaries.
This code use for get value from dictionary value without knowing key and value..
var json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonString );
var javSer = new JavaScriptSerializer();
var dfi = javSer.Deserialize<Dictionary<string, string>>(json);
string dataString= dfi.Values.First();
How can you possibly know which value you need if you don't have the key?
A Dictionary in .NET does contain a Keys and Values collection, so if you are only interested in the values, you can use that.

How to access a Dictionary values within a list by a class object?

I've a class like as below:
public class Source
{
...
...
public List<Dictionary<int, int>> blanks { get; set; }
}
I've created an object of this and a Dictionary for it. I filled 'dic' Dictionary. Then, I add this dic to the blanks list.
Source src = new Source();
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(30, 50);
dic.Add(40, 60);
src.blanks.Add(dic);
And I try to access these 'Key' and 'Value' elements. But, I can't.
int a = src.blanks[0].Key;
int b = src.blanks[0].Value;
What can I do to access these elements?
Thanks.
src.blanks[0] is a whole dictionary, not a single KeyValuePair<int,int>. That is why you cannot access a .Key or .Value on it - there are potentially many keys, and many values associated with them.
You can access all key-value pairs in a dictionary at position zero by enumerating them, like this:
foreach (var kvp in src.blanks[0]) {
int a = kvp.Key;
int b = kvp.Value;
Console.WriteLine("Key:{0} Value:{1}", a, b);
}
blanks[0] returns a Dictionary<int, int>, you need to specify key of your item.
src.blanks[0][key]
Or loop through your values:
foreach(var pair in src.blanks[0])
{
int currentKey = pair.Key;
int currentValue = pair.Value;
}
You are trying to access a dictionary in the list which has no Key property. The Keyvaluepairs in a dictionary have keys.
So assuming you want to look into the first dictionary in the list:
Dictionary<int, int> dict = s.blanks[0];
// lookup 30:
int value = dict[30]; // 40
to get the value you should first index the list and then index the dictionary
to get value
int b = (src.blanks[0])[0]
You want something like the following:
var listItem = src.blanks[0];
var dictionaryItem = listItem[0];
var a = dictionaryItem.Key;
var b = dictionaryItem.Value;
Nevertheless, i advice you to get rid of those "nested generics" List<Dictionary<..,..>. You won't be able to distinguish what kind of object you're dealing with if you use these nested structs.
Use other structs that better represent your business logic. For example, you could derive your own class from List

How to assign key=>value pairs in a Dictionary?

Here is my code:
string[] inputs = new[] {"1:2","5:90","7:12","1:70","29:60"};
//Declare Dictionary
var results = new Dictionary<int, int>();
//Dictionary<int, int> results = new Dictionary<int, int>();
foreach(string pair in inputs)
{
string[] split = pair.Split(':');
int key = int.Parse(split[0]);
int value = int.Parse(split[1]);
//Check for duplicate of the current ID being checked
if (results.ContainsKey(key))
{
//If the current ID being checked is already in the Dictionary the Qty will be added
//Dictionary gets Key=key and the Value=value; A new Key and Value is inserted inside the Dictionary
results[key] = results[key] + value;
}
else
{
//if No duplicate is found just add the ID and Qty inside the Dictionary
results[key] = value;
//results.Add(key,value);
}
}
var outputs = new List<string>();
foreach(var kvp in results)
{
outputs.Add(string.Format("{0}:{1}", kvp.Key, kvp.Value));
}
// Turn this back into an array
string[] final = outputs.ToArray();
foreach(string s in final)
{
Console.WriteLine(s);
}
Console.ReadKey();
I want to know if the difference if there is between assigning a key=>value pair in a dictionary.
Method1:
results[key] = value;
Method2:
results.Add(key,value);
In method 1, the function Add() was not called but instead the Dictionary named 'results' assigns somehow sets a Key-Value pair by stating code in method1, I assume that it somehow adds the key and value inside the dictionary automatically without Add() being called.
I'm asking this because I'm currently a student and I'm studying C# right now.
Sir/Ma'am, your answers would be of great help and be very much appreciated. Thank you++
The Dictionary<TKey, TValue> indexer's set method (the one that is called when you do results[key] = value;) looks like:
set
{
this.Insert(key, value, false);
}
The Add method looks like:
public void Add(TKey key, TValue value)
{
this.Insert(key, value, true);
}
The only difference being if the third parameter is true, it'll throw an exception if the key already exists.
Side note: A decompiler is the .NET developers second best friend (the first of course being the debugger). This answer came from opening mscorlib in ILSpy.
If the key exists in 1) the value is overwritten. But in 2) it would throw an exception as keys need to be unique

Item duplication problem

My goal is to add a insert new value to a column where my column values are as follows
100 * 100
150 * 150
200 * 200
200 * 200
I get the following error:
Item has already been added. Key in dictionary: '200 x 200' Key being added: '200 x 200'
For next code:
SortedList sortedList = new SortedList();
foreach (ListItem listItem in ddldimension.Items)
sortedList.Add(listItem.Text, listItem.Value);
if (!sortedList.ContainsKey(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1")))
sortedList.Add(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1"), "defaultEmbedDimension1");
if (!sortedList.ContainsKey(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension2")))
sortedList.Add(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension2"), "defaultEmbedDimension2");
if (!sortedList.ContainsKey(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension3")))
sortedList.Add(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension3"), "defaultEmbedDimension3");
From the error message you're getting, and from the documentation for SortedList:
In either case, a SortedList does not allow duplicate keys.
So it would appear that a SortedList isn't the right structure for you to be using in your application. Unfortunately, you've provided insufficient information to allow me to suggest something better.
SortedList does not allow adding duplicate keys. Use List<> (along with KeyValuePair for example) instead (eg. List<KeyValuePair<string, object>>).
Here is the solution for your code:
var list = new List<KeyValuePair<string, string>>();
foreach (var item in ddldimension.Items)
{
list.Add(new KeyValuePair<string, string>(item.Text, item.Value));
}
var defaultEmbedDimension1 = CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1");
int index = list.FindIndex(k => k.Key == defaultEmbedDimension1); // If there is no such Key, it will be -1. If you want to find by Value, replace k.Key by k.Value
if (index >= 0)
{
list.Add(new KeyValuePair<string, string>(defaultEmbedDimension1, "defaultEmbedDimension1"));
}
In this way, you allow to keep duplicate keys in your structure. Note you invoke the same method twice. Initialize variable instead:
string defaultEmbedDimension1 = CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1");
To populate list, you can alternatively use LINQ:
var list = ddldimensions.Items.Select(item => new KeyValuePair<string, string>(item.Text, item.Value)).ToList();
Read also: C# KeyValuePair Collection Hints at Dot Net Perls.
But if you decide to disallow duplicates and gently deal with them in SortedList, you can create an extension:
public static class SortedListExtensions
{
public static bool AddIfNotContains<K, V>(this IDictionary<K, V> dictionary, K key, V value)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
return true;
}
return false;
}
}
And use it as I did below, without throwing exception:
var sortedList = new SortedList<string, string>();
sortedList.Add("a", "b");
sortedList.AddIfNotContains("a", "b"); // Will not be added
sortedList.AddIfNotContains("b", "b"); // Will be added

Categories

Resources