Dictionary Manipulations in C# - c#

I want to store the following values into a dictionary:
key (string) - values (list of strings)
aaa - myfirstvalue1
aaa - myfirstvalue2
bbb - myfirstvalue3
ccc - myfirstvalue4
ccc - myfirstvalue5
Dictionary:
Dictionary<string, List<string> myvalues = new Dictionary<string, List<string>>();
I tried to store these values but I got the duplicate key error.

Dictionaries have the feature that a key can be added only once. You have the right type, but the way you add the data matters.
You can initialize the dictionary with the data provided like this:
Dictionary<string, List<string>> myvalues = Dictionary<string, List<string>>
{
{ "aaa", new List<string> { "myfirstvalue1", "myfirstvalue2" } },
{ "bbb", new List<string> { "myfirstvalue3" } },
{ "ccc", new List<string> { "myfirstvalue4", "myfirstvalue5" } },
};
With this, each key has one list of strings assigned to it. You can add more values like this:
var key = "aaa"; // for example
if (myvalues.ContainsKey(key)
{
myvalues[key].Add("new value");
}
else
{
myvalues.Add(key, new List<string> { "new value" });
}
You can retrieve values like this:
List<string> aaaVals = myvalues["aaa"];
and then convert that List<string> to an Array with List.ToArray().

Related

Extracting variable data from JSON in C#

I have the following JSON which I need to manipulate into a different JSON format to be consumed by another process. My data is variable to some extent. Here is an example:
{
"subform_12": {
"multiline_2": "Subform 1 Long Text",
"listpicker_5": "High",
"alpha_1": "SubForm 1 Text"
},
"subform_13": {
"multiline_2": "Subform 2 Long Text",
"alpha_1": "SubForm 2 Text"
}
}
The variable part is the name of the json object (eg "subform_13") and the number and content of name pairs per object (eg "multiline_2": "Subform 1 Long Text").
What I need to do is convert each node into its own chunk of json, as in the following format:
{
"subform_13": [
[{
"fieldKey": "multiline_2",
"value": "Subform 2 Long Text"
},
{
"fieldKey": "alpha_1",
"value": "SubForm 2 Text"
}
]
]
}
Then separately:
{
"subform_13": [
[{
"fieldKey": "multiline_2",
"value": "Subform 2 Long Text"
},
{
"fieldKey": "alpha_1",
"value": "SubForm 2 Text"
}
]
]
}
So far I see that I can iterate thru the list as follows:
var json = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(
jsonString,
new Newtonsoft.Json.JsonSerializerSettings()
{
DateParseHandling = Newtonsoft.Json.DateParseHandling.None,
});
foreach (var item in json)
{
// I can see the "subform_13" and contents here in item , how do I generically extract them?
}
Any help appreciated.
Here is your Main method augmented with the ability to iterate through all values:
static void Main(string[] args)
{
var json = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string,JObject>>(
jsonString,
new Newtonsoft.Json.JsonSerializerSettings()
{
DateParseHandling = Newtonsoft.Json.DateParseHandling.None,
});
foreach (var item in json)
{
var key = item.Key; // "subform_12"
var val = item.Value;
Console.WriteLine(key+":");
foreach (var field in val)
{
var fieldKey = field.Key; // e.g. "multiline_2"
var fieldVal = field.Value; // e.g. "Subform 1 Long Text"
Console.WriteLine($"{fieldKey}={fieldVal.Value<string>()}");
}
Console.WriteLine();
}
}
I am just printing the values out; you would construct your new objects - for example as dynamic - using these values.
The output of my Main is:
subform_12:
multiline_2=Subform 1 Long Text
listpicker_5=High
alpha_1=SubForm 1 Text
subform_13:
multiline_2=Subform 2 Long Text
alpha_1=SubForm 2 Text
Hope it helps.
There are probably more elegant ways using linq, but here's code using a plain old JavaScriptSerializer from System.Web.Extensions.
There is a result dictionary, which you probably don't need if you want each object separated.
The json strings for each object is stored in the allJson list.
Similary, if you want the dictionary objects themselves you could just add seperated to a list during each iteration.
string s = "{\"subform_12\":{\"multiline_2\":\"Subform 1 Long Text\",\"listpicker_5\":\"High\",\"alpha_1\":\"SubForm 1 Text\"},\"subform_13\":{\"multiline_2\":\"Subform 2 Long Text\",\"alpha_1\":\"SubForm 2 Text\"}}";
JavaScriptSerializer ser = new JavaScriptSerializer();
Dictionary<string, object> obj = ser.DeserializeObject(s) as Dictionary<string, object>;
// combined dictionary of all results
Dictionary<string, object> result = new Dictionary<string, object>();
// an intermediary dictionary to house the results of each object
Dictionary<string, object> separated = new Dictionary<string, object>();
// a list to hold the json representation of each separate object
List<String> allJson = new List<string>();
foreach (KeyValuePair<string, object> src in obj)
{
Dictionary<string, object> children = (Dictionary<string, object>)src.Value;
Dictionary<string, object> t = new Dictionary<string, object>();
separated = new Dictionary<string, object>();
List<object> l = new List<object>();
foreach (KeyValuePair<string, object> child in children)
{
t.Add("fieldKey", child.Key);
t.Add("value", child.Value);
l.Add(t);
t = new Dictionary<string, object>();
}
separated.Add(src.Key, l.ToArray());
allJson.Add(ser.Serialize(separated));
result.Add(src.Key, l.ToArray());
}
// final string containing all transformed objects combined.
string combined = ser.Serialize(result);

Get KeyValuePair given Key

Given a String that is a Key contained in Dictionary<String, List<String>>, how do I retrieve the KeyValuePair<String, List<String>> that corresponds to that Key?
The problem with other answers using FirstOrDefault is that it will sequentially search the entire dictionary until it finds a match, and you lose the benefit of having a hashed lookup. It seems more sensible if you really need a KeyValuePair to just build one, like this:
public class Program
{
public static void Main(string[] args)
{
var dictionary = new Dictionary<string, List<string>>
{
["key1"] = new List<string> { "1" },
["key2"] = new List<string> { "2" },
["key3"] = new List<string> { "3" },
};
var key = "key2";
var keyValuePair = new KeyValuePair<string, List<string>>(key, dictionary[key]);
Console.WriteLine(keyValuePair.Value[0]);
}
}
(with credit to David Pine for the original code in his answer).
Here's a fiddle for that: https://dotnetfiddle.net/Zg8x7s
Usually you want the value associated with the key, for example:
Dictionary<String, List<String>> dictionary = GetDictionary();
var value = dictionary["key"];
But you can use Linq to get the entire KeyValuePair:
var dictionary = new Dictionary<string, List<string>>
{
["key1"] = new List<string> { "1" },
["key2"] = new List<string> { "2" },
["key3"] = new List<string> { "3" },
};
var keyValuePair = dictionary.FirstOrDefault(kvp => kvp.Key == "key2");
Console.WriteLine(keyValuePair?.Value[0]); // Prints "2"
Here is a .NET Fiddle.

What's the correct syntax to populate a Dictionary having nested Lists as value?

I am new to C# and I am trying to define a Dictionary having:
as key:
a string
as value:
a List of Lists of strings.
What I could came up (not entirely sure it's right?) is this:
var peopleWithManyAddresses = new Dictionary<string, List<List<string>>> {};
Now, if the above is right, I would like to know how to populate one item of peopleWithManyAddresses.
Intellisense is telling me that the following is correct only up until "Lucas":
peopleWithManyAddresses.Add("Lucas", { {"first", "address"}, {"second", "address"} });
What's the correct syntax for it?
P.S. I know I could use a class, but for learning purposes I'd like to do it this way for now.
To initialize the List<List<string>> object, you have to use the new List<List<string>> { ... } syntax. And to initialize each sub list you have to use a similar syntax, i.e. new List<string> {... }. Here is an example:
var peopleWithManyAddresses = new Dictionary<string, List<List<string>>>();
peopleWithManyAddresses.Add("Lucas", new List<List<string>>
{
new List<string> { "first", "address" },
new List<string> { "second", "address" }
});
Your initialization statement is correct.
Using C# 6.0, you can use the following syntax to populate one item:
var dict = new Dictionary<string, List<List<string>>>
{
["Lucas"] = new[]
{
new[] { "first", "address" }.ToList(),
new[] { "second", "address" }.ToList(),
}.ToList()
};
You could use the following to populate two items:
var dict = new Dictionary<string, List<List<string>>>
{
["Lucas"] = new[]
{
new[] { "first", "address" }.ToList(),
new[] { "second", "address" }.ToList(),
}.ToList(),
["Dan"] = new[]
{
new[] { "third", "phone" }.ToList(),
new[] { "fourth", "phene" }.ToList(),
}.ToList(),
};
If you want to add more data later, you can do the following:
dict["Bob"] = new[]
{
new[] { "fifth", "mailing" }.ToList(),
new[] { "sixth", "mailing" }.ToList(),
}.ToList();
first I create List separated from Dictionary:
List<string> someList = new List<string<();
var otherList = new List<List<string>>();
var peopleWithManyAddresses = new Dictionary<string, List<List<string>>> {};
First add strings in someList
someList.Add("first");
someList.Add("addresss");
Then add in otherList:
otherList.Add(someList);
Now create new List of strings:
var thirdList = new List<string>();
thirdList.Add("second");
thirdList.Add("addresss");
And add the last list of strings in other list and add in dictionary
otherList.Add(thirdList);
peopleWithManyAddresses.Add("Lucas", otherList);

Create an array of list

I want to create a array of list which will contain a string and a list of arrays.
example:
I want like this one.
list(0) --- string value list(0) ---list(0) - string value
list(0) ----list(1) - string value
list(1) --- string value list(1) ---list(0) - string value
list(1) ----list(1) - string value
and so on..
how will i declare?
i tried:
List<List<String>> list = new List<List<string>>(); // but it didn't work.
List<string[]> arrayList = new List<string[]>(); // again it didn't work..
is this possible to declare?
if so how?
Isnt this a Dictionary<string, string[]>?
var x = new Dictionary<string, string[]>();
x.Add("string1", new string[] {"a", "b", "c"})
Then you can have a list of that dictionary.
var list = new List<Dictionary<string, string[]>>();
list.Add(x);
Does this work for you?
public class Tree<T> : List<Tree<T>>
{
public Tree(T value) { this.Value = value; }
public T Value { get; set; }
}
It's not an array, but a list, but it's much the same structure.
You can then assign it like this:
var trees = new []
{
new Tree<string>("Branch 1")
{
new Tree<string>("Leaf 1.1"),
new Tree<string>("Leaf 1.2"),
},
new Tree<string>("Branch 2")
{
new Tree<string>("Leaf 2.1"),
new Tree<string>("Leaf 2.2"),
},
};
As I can see in your data structure you've asked for A List containing two List's and all of them are of same string type then you should go with Dictionary. As a List can of of single type and you can add a single value to it at a time. Try Dictionary,
Dictionary<string, string> dictionary = new Dictionary<string, string>();
or if you want a Dictionary containing List of string,
Dictionary<List<string>, List<string>> dictionary = new Dictionary<List<string>, List<string>>();
try this
List<List<String>> str_2d_list = new List<List<String>>();
List<String> l1 = new List<string>();
l1.Add("l1.string1");
l1.Add("l1,string2");
List<String> l2 = new List<string>();
l2.Add("l2.string1");
l2.Add("l2,string2");
str_2d_list.Add(l1);
str_2d_list.Add(l2);
if you want to create an array of a string and a list, use the second way in the code. but if you want a list of list use first way in the code.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
// an example of list of strings
List<string> names = new List<string>();
names.Add("Mike");
names.Add("Sarah");
List<string> families = new List<string>();
families.Add("Ahmadi");
families.Add("Ghasemi");
// 1st way
List<List<string>> outsideList = new List<List<string>>();
outsideList.Add(names);
outsideList.Add(families);
// 2nd way
Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();
d.Add("first", names);
d.Add("second", families);
// how to access list<list<>>
foreach (List<string> list in outsideList)
{
foreach (string s in list)
Console.WriteLine(s);
}
// how to access list inside dictionary
foreach (List<string> list in d.Values)
{
foreach (string s in list)
Console.WriteLine(s);
}
}
}
}

How to create and populate a nested dictionary with three keys

I have a unique double corresponding to a variation of three strings. I want to populate a dictionary or something such that I can call something like dict[key1][key2][key3] and get the value.
I've tried a whole bunch of things like
Dictionary<string, Dictionary<string, double>> dict = new Dictionary<string, Dictionary<string, double>> {
{ "Foo", {"Bar", 1.2 } },
{ "Foo", {"Test", 3.4 } }
};
Which gives me syntax errors and errors like "Error 4 A namespace cannot directly contain members such as fields or methods"
And
Dictionary<double, Tuple<string, string>> dict = {
{1.23, "Blah", "Foo"}
};
Which gives me errors like "Error 1 Can only use array initializer expressions to assign to array types. Try using a new expression instead."
And
object dict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
dict["k1"] = new Dictionary<string, Dictionary<string, string>>();
dict["k1"]["k2"] = new Dictionary<string, string>();
dict["k1"]["k2"]["k3"] = 3.5;
Which gives me syntax errors and errors like "Error 2 Invalid token '"k1"' in class, struct, or interface member declaration"
How should I go about this? Thanks in advance.
![enter image description here][1]
Edit: Trying Jonesy's code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string[] grades = { "Grade 1", "Grade 5", "Grade 8", "ASTM A325", "316 Stainless", "Monel", "Brighton Best 1960" };
string[] sizes = { "#1", "2", "3", "4", "5", "6", "8", "10", "12", "1/4", "5/16", "3/8", "7/16", "1/2", "9/16", "5/8", "3/4", "7/8", "1", "1-1/8", "1-1/4", "1-3/8", "1-1/2" };
var dict = new Dictionary<string, Dictionary<string, Dictionary<string, double>>>();
dict["k1"] = new Dictionary<string, Dictionary<string, double>>();
dict["k1"]["k2"] = new Dictionary<string, double>();
dict["k1"]["k2"]["k3"] = 3.5;
public Form1()
{
InitializeComponent();
}
your last attempt is close, you want:
var dict = new Dictionary<string, Dictionary<string, Dictionary<string, double>>>();
dict["k1"] = new Dictionary<string, Dictionary<string, double>>();
dict["k1"]["k2"] = new Dictionary<string, double>();
dict["k1"]["k2"]["k3"] = 3.5;
you want var instead of object
(or Dictionary<string, Dictionary<string, Dictionary<string, double>>> if you like scrolling)
and your very last string should be a double.
As I understood, you have data and want to perform lookup in it. Why can't you just use some database for that purpose?
But if you really want to hardcode all values, you can. Just don't initialize dictionary manually, make simplifications - parse data in runtime.
Something like this. (I suppose, that you are novice in c# programming, so I've created new Console Application and copy-pasted all the code for your convenience)
public class Program
{
// harcode all data as string
const string RawData =
"k11,k12,k13=3.4;" +
"k21,k22,k23=4.42;" +
"k31,k32,k33=5.91;" +
"k41,k42,k43=8.14;" +
"k51,k52,k53=4.13;" +
"k61,k62,k63=5.4";
static void Main(string[] args)
{
// create dictionary from hardcoded string
var data = ParseData();
// use Tuple as key for data lookup
var value = data[Tuple.Create("k11", "k12", "k13")];
// check, that value equals expected one
Debug.Assert(value == 3.4);
}
private static IDictionary<Tuple<string, string, string>, double> ParseData()
{
var parsedData =
RawData
.Split(';')
.Select(ParseRow)
.ToDictionary(x => x.Item1, x => x.Item2);
return parsedData;
}
private static Tuple<Tuple<string, string, string>, double> ParseRow(string row)
{
var parts = row.Split('=');
var coefficients = ParseCoefficients(parts[0]);
var value = Double.Parse(parts[1], CultureInfo.InvariantCulture);
return Tuple.Create(coefficients, value);
}
private static Tuple<string, string, string> ParseCoefficients(string row)
{
var coeffs = row.Split(',');
var result = Tuple.Create(coeffs[0], coeffs[1], coeffs[2]);
return result;
}
}
As another simplification, you can use custom class as dictionary key instead of nested dictionaries. Write your own(pay attention, that it should override equality members Equals and GetHashCode), or use something from base class library. Tuple<string, string, string> is the perfect one.

Categories

Resources