I have a Hashtable that I am trying to log the values for. the name of the Hashtable is "props".
My code is as follows:
Dictionary<string, string> keyPairs = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> items in props)
{
keyPairs.Add(items.Key, items.Value);
}
Logging.Instance.WriteInformation(string.Format("Key: {0} \t Value: {1}", keyPairs.Keys, keyPairs.Values));
However this results in a InvalidCastException at runtime.
Is there an easier/more sensible way to log key/value pairs?
Ideally the output would look something like so:
key1 value1
key2 value2
key3 value3
etc.
As an addition thought, in debugging, the exception seems to occur right at the start of the foreach loop. I have also tried setting it up as KeyValuePair<string, object> but I get the same InvalidCastException.
Would this possibly have something to do with KeyValuePair being inside System.Collections.Generic and Hashtable being inside System.Collections?
You can either use a loop or, if you want an one-liner:
var allPairs = string.Join(Environment.NewLine,
keyPairs.Select(kvp => string.Format("Key: {0} \t Value: {1}", kvp.Key, kvp.Value)));
Logging.Instance.WriteInformation(allPairs);
Sure, just loop:
for (var entry : keyPairs)
{
Logging.Instance.WriteInformation(string.Format("Key: {0} \t Value: {1}",
entry.Key, entry.Value);
}
It's only a few lines - you could easily put it in a method if you need it in more than one place.
Log while you are looping.
Dictionary<string, string> keyPairs = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> items in props)
{
keyPairs.Add(items.Key, items.Value);
Logging.Instance.WriteInformation(string.Format("Key: {0} \t Value: {1}", items.Key, items.Value));
}
Related
I’m a rookie in programming and I have a problem understanding how to print elements from a List.
In the task I’ve been given, I receive:
List<Dictionary<string,string>>() list = new
List<Dictionary<string,string>>();
list.Add(processString(string, string));
list.Add(processString(string, string));
The processStrig is a Dictionary<string,string> and the keys are the same for both records.
I tried to create a new Dictionary and then populate it with foreach:
Dictionary<string,string>() dict = new Dictionary<string, string>();
foreach (Dictionary<string,string>r in list)
{
foreach (string inner in r.Keys)
{
if (!dict.ContainsKey(inner))
{
dict.Add(inner, r[inner]);
}
}
}
and then print the new dict with another foreach, but it shows me only the first input because the keys are the same. So basically my question is how to print the both inputs? The output should look like this:
The output should look like this:
[0]
"count":"some string"
"order":"some string"
[1]
"count":"some other string"
"order":"some other string"
If you are looking for a loop solution, you can try something like this:
List<Dictionary<string, string>> list = ...
for (int i = 0; i < list.Count; ++i) {
Console.WriteLine($"[{i}]");
if (list[i] == null)
Console.WriteLine("[null]");
else
foreach (var pair in list[i])
Console.WriteLine($"\"{pair.Key}\" : \"{pair.Value}\"");
}
Let's have a method that makes you a dictionary:
public static Dictionary<string, string> MakeMeADictionary(string value1, string value2){
var d = new Dictionary<string, string>();
d["key1"] = value1;
d["key2"] = value2;
return d;
}
Let's call it twice, adding the results to a List:
var listOfDicts = new List<Dictionary<string, string>>();
listOfDicts.Add(MakeMeADictionary("first val", "second val"));
listOfDicts.Add(MakeMeADictionary("third val", "fourth val"));
Let's enumerate the list, and then each dictionary inside it:
foreach(var dict in listOfDicts){
Console.WriteLine("Enumerating a dictionary");
foreach(var keyValuePair in dict)
Console.WriteLine($"Key is: {keyValuePair.Key}, Value is: {keyValuePair.Value}");
}
Result:
Enumerating a dictionary
Key is: key1, Value is: first val
Key is: key2, Value is: second val
Enumerating a dictionary
Key is: key1, Value is: third val
Key is: key2, Value is: fourth val
Strive for variable names that make your code make sense; plurals or names of colelction types for collections, foreach vars that singularly make sense for the plural being enumerated etc.. If this were a less contrived example, and e.g. it were a List<Person> I'd call it people, perhaps, and have foreach(var person in people).. I couldn't understand your choice of r in foreach(var r in list)
I'm at the first step in programming and i'm stuck with a problem with Dictionary(key value) pair.
The statement of the problem is:
Write a console application that extracts and prints the key and value on a line.
Example:
For input data:
year:2018
The console will display:
year
2018
here is my code:
string inputData = Console.ReadLine();
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add(inputData, 2018 );
foreach (KeyValuePair<string, int> kvp in dictionary)
{
Console.WriteLine("{0}\n{1}", kvp.Key, kvp.Value);
}
// expects year:2018
var inputData = Console.ReadLine();
// split by ':' to get 'year' and '2018' values
var values = inputData.Split(':');
// creates a dictionary
var dictionary = new Dictionary<string, int>();
// add the 'year' string as key and '2018' as value
dictionary.Add(values[0], Convert.ToInt32(values[1]));
// print all the dictionary
foreach (var kvp in dictionary)
{
Console.WriteLine("{0}\n{1}", kvp.Key, kvp.Value);
}
However, the problem description is not asking you to use a dictionary.
So, instead of creating a dictionary, you can simply print the values.
var inputData = Console.ReadLine();
var values = inputData.Split(':');
Console.WriteLine(values[0]);
Console.WriteLine(values[1]);
So I'm trying to print out the members of my list. I'm using the following dictionary-structure: SortedDictionary<string,List<int>> where i'm using a string as the key.
In my function ShowContents I'm trying to print out what entry i'm looking at, and the amount of elements, as well as what the elements are. This is where i'm struggling. I'm just getting System.Collections.Generic.List1[System.Int32]instead of the objects.
Here's my current code:
SortedDictionary<string,List<int>> jumpStats = new SortedDictionary<string,List<int>>(); // jumpstats[0] == (volt, 10m)
public string ShowContents()
{
var sb = new StringBuilder();
foreach (KeyValuePair<string, List<int>> item in jumpStats)
{
sb.Append(string.Format("{0}: has {1} entries with values {2}", item.Key, item.Value.Count(), item.Value));
}
return sb.ToString();
}
public SortedDictionary<string,List<int>> addjumpStats() //Adding information about the jump to the dictionary
{
try
{
jumpStats.Add("Volt", new List<int>());
jumpStats["Volt"].Add(12);
jumpStats["Volt"].Add(13);
jumpStats["Volt"].Add(15);
}
catch (ArgumentException)
{
Console.WriteLine("An Element already exists with the same key");
}
return jumpStats;
}
Example output right now: Volt: 3 System.Collections.Generic.List1[System.Int32]
In your append function you're outputting item.Value which is a List<int> hence why you are seeing the class name - the ToString function of a List does not know to concatenate all the values in the list together - it merely returns the class name. You need to tell it what to do. An easy way to do this is to use string.join:
string.Join(",", item.Value)
And in context:
var sb = new StringBuilder();
foreach (KeyValuePair<string, List<int>> item in jumpStats)
{
sb.Append(string.Format("{0}: has {1} entries with values {2}", item.Key, item.Value.Count(), string.Join(",", item.Value));
}
return sb.ToString();
I have a Dictionary<string, List<string>>, I used to get back to value using
List<string> keyDicoList = DictErrorCorrectionBeforeJS.Keys.ToList();
List<string> listValue = DictErrorCorrectionBeforeJS.SelectMany(x => x.Value).ToList();
But it seems on production ae using ASP.NET 2.0 when we Dev in 4.0...
So I try to recover them with another way.I find for the Key :
foreach (string key in DictErrorCorrection.Keys)
{
keyDicoList.Add(key);
}
But i have trouble getting the value. As it is not a simple value but a List inside the Dictionnary. I can't do a foreach on it and it seems as it is an enumerator that I can't just go for a for loop, with just adding the Dico.Values[x] to my listValue
How should I proceed to get all the dictionary.value inside my listValue ?
You can just iterate over them using a foreach. The dictionary enumerator yields a KeyValuePair<string, List<string>>:
foreach (KeyValuePair<string, List<string>> kvp in dictionary)
{
string key = kvp.Key;
List<string> list = kvp.Value;
foreach (string listItem in list)
{
// .. use the item, or skip the foreach and use the list at once
}
}
Typically, you enumerate the KVP set of the dictionary:
List<string> values = new List<string>();
foreach (KeyValuePair<string, List<string>> kvp in dict)
{
values.AddRange(kvp.Value); // kvp.Value is List<string>
}
I have declared a dictionary like this:
Dictionary<string, KeyValuePair<string, string>> dc = new Dictionary<string, KeyValuePair<string, string>>();
now how can I loop through it? I wanted something like the following so I created that dictionary:
name1
oldValue1
newValue1
name2
oldValue2
newValue2
...
You can loop through it like so
foreach (var pair in dc)
{
string name = pair.Key;
string oldValue = pair.Value.Key;
string newValue = pair.Value.Value;
// use the values
}
But I have a feeling you're using the wrong tool for the job. It sounds to me like you really need to go ahead and define a proper class to hold the names and values, and then just work with a List<T> of that class.
foreach( KeyValuePair<string, string> kvp in dc )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
When you loop a dictionary you use KeyValuePair that is generic. Since your dictionary contain the key as string and the value as string, this one will take also a string for both.
You can access the key with kvp.Key and the value with kvp.Value.
For your example, you are using a Dictionary of string that contain a value of KeyValuePair.
So, you can have the exact print you want with :
foreach( KeyValuePair<string, KeyValuePair<string,string>> kvp in dc )
{
Console.WriteLine(kvp.Key + " " + kvp.Value.Key + " "+ kvp.Value.Value);
}