Dictionary<string, list<string>> iteration ASP.NET2.0 - c#

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>
}

Related

C# How can I print all elements from List<Dictionary<string,string>> on the console?

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)

Iterating over KeyValuePair (as a key in dictionary) is not working

Following is my function:-
public void deleteAllCarsWithType(Dictionary < KeyValuePair<string, string> , car > dictCar, string typeOfCar)
{
var keyValuePairs = dictCar.Where(keyValuePair => keyValuePair.Key.Equals(typeOfCar));
//var count = keyValuePairs.Count();
//this also gives the size of keyValuePairs as 0.
foreach (var keyValuePair in keyValuePairs)
{
dictCar.Remove(keyValuePair.Key);
}
}
KeyValuePair<string, string> which is the key of the dictionary, has carType as key and carModel as the model of the car.
When I run the unit test of it
[TestMethod]
public void DeleteSameCarTypeTest()
{
Dictionary<KeyValuePair<string, string>, car> Dictionary = new Dictionary<KeyValuePair<string, string>, car>();
Dictionary.Add(new KeyValuePair<string, string>("x", "y"), new car());
Dictionary.Add(new KeyValuePair<string, string>("x", "z"), new car());
new Program().deleteAllCarsWithType(Dictionary, "x");
Assert.AreEqual(0, Dictionary.Count);
}
It fails by saying Expected 0 and Actual 2.
When I debug it, I found out that keyValuePairs have the expected two values (which are with carType "x") but when I step into the foreach loop it's not going in and saying that count of keyValuePairs is 0. Then I tried to get the Count of the keyValuePairs which is also coming 0. I don't know what I am doing wrong. Please help me out.
keyValuePair.Key is a KeyValuePair.
You need:
var keyValuePairs = dictCar.Where(keyValuePair => keyValuePair.Key.Key.Equals(typeOfCar));
In fact a few more changes need to be made as others have pointed out:
private static void deleteAllCarsWithType(Dictionary<KeyValuePair<string, string>, car> dictCar, string typeOfCar)
{
var keyValuePairs = dictCar.Where(keyValuePair => keyValuePair.Key.Key.Equals(typeOfCar)).ToArray();
foreach (var keyValuePair in keyValuePairs)
{
dictCar.Remove(keyValuePair.Key);
}
}
You are looking into the values as opposed to the keys.
Change your code to this:
private static void deleteAllCarsWithType(Dictionary<KeyValuePair<string, string>, car> dictCar, string typeOfCar)
{
var keyValuePairs = dictCar.Keys.Where(keyValuePair => keyValuePair.Key.Equals(typeOfCar)).ToArray();
foreach (var keyValuePair in keyValuePairs)
{
dictCar.Remove(keyValuePair);
}
}
Notice I am also converting the query to an array. Otherwise the loop will fail because you modify the collection.

single key but multiple value different row

I have
Dictionary idDictionary = new Dictionary();
within this Dictionary, I want, add two row with same key but values different
I used this Dictionary in many methods that a global Dictionary in one method has that issue. if I changed like all u suggest another method get error
Try something like this.
Dictionary<string, List<string>> obj = new Dictionary<string, List<string>>();
List<string> keyValue = new List<string>();
keyValue.Add("a");
keyValue.Add("b");
obj.Add("key1", keyValue);
foreach (KeyValuePair<string, List<string>> entry in obj)
{
// do something with entry.Value or entry.Key
}
List<string> keyValueOutput = new List<string>();
if (obj.TryGetValue("key1",out keyValueOutput))
{
foreach (string s in keyValueOutput)
{
// do something with entry.Value or entry.Key
}
}

Making a List<string> from Dictionary<string, string>

I was wondering if it were possible to make a list from the dictionary values where the key is a specified value?
The dictionary would like this:
Sidcup - DPC1
Sidcup - DPC2
Blackheath - DPC3
Blackheath - DPC4
Bexleyheath - DPC5
In fact, I'm not entirely implementing a Dictionary as above is a good idea. Here is its implementation:
DataSet ds = EngineBllUtility.GetDPCsForImportFile(connectionString, fileID);
if (ds.Tables.Count > 0)
{
DataTable dtDPCs = EngineBllUtility.GetDPCsForImportFile(connectionString, fileID).Tables[0];
Dictionary<string, string> preliminaryList = new Dictionary<string, string>();
if (dtDPCs.Columns.Contains("DPCNumber") && dtDPCs.Columns.Contains("BranchName"))
foreach (DataRow dataRow in dtDPCs.Rows)
{
preliminaryList.Add(dataRow["BranchName"].ToString(), dataRow["DPCNumber"].ToString());
}
I have the following code: (Excuse the last line, its just so you have an idea of what I'm trying to do).
foreach (string branch in branchNames)
{
string subfolder = System.IO.Path.Combine(saveLocation, branch);
System.IO.Directory.CreateDirectory(subfolder);
List<string> certificateList = new List<string>();
certificateList.Add(DPCNumber in preliminaryList where Key = branch);
}
In the above the branch is the key from the Dictionary. I need to iterate through because it needs to create a new folder and then do something with the certificateList I am creating.
Sure:
private static void TestZip()
{
Dictionary<string, string> stringstringdic = new Dictionary<string, string>();
stringstringdic.Add("1", "One");
stringstringdic.Add("2", "Two");
stringstringdic.Add("3", "Three");
stringstringdic.Add("4", "Four");
stringstringdic = stringstringdic.Where(pair => pair.Key != "1")
.ToDictionary(pair => pair.Key, pair => pair.Value);
List<string> stringlist = stringstringdic.Keys.Concat(stringstringdic.Values).ToList();
foreach (string str in stringlist)
{
Console.WriteLine(str);
}
}
//Output:
//2
//3
//4
//Two
//Three
//Four
Of course, you'll have to change the Where clause to reflect your real need.
If I understood you right, it's like .Where(pair => pair.Key == branch)
If I understand you correctly you want to add the value based on a key to a separate List?
certificateList.Add(preliminaryList[branch])
This is simplified as I really need to see the declaration of preliminaryList to know how DPCNumber fits into all of it. Could it be...
certificateList.Add(preliminaryList[branch].ToString())
To simply create a list of keys you can do the following.
var dictionary = new Dictionary<string, string>();
dictionary.Add("key1", "value1");
dictionary.Add("key2", "value2");
dictionary.Add("key3", "value3");
dictionary.Add("key4", "value4");
dictionary.Add("key5", "value5");
var list = dictionary.Keys.ToList();
This should give you a list with values "key1", "key2", "key3", "key4", "key5".
You can put a where clause in to filter out certain keys. The following gives all keys which contain a "2" (random example), resulting in just "key2".
var filteredList = dictionary.Keys.Where(key => key.Contains("2")).ToList();
Edit:
To get a value given a specific key.
string value = dictionary["key1"];
Note, the key is a dictionary must be unique, so for a given key you will only ever get a single value back and not a list of values.

C# Foreach Loop Hashtable Issue

I have some code which populates a hashtable with a question as the key and an arraylist of answers as the value.
I want to then print out these values from the hashtable so that it displays the question and corresponding solutions for each individual question in the hashtable.
I know I have done something totally stupid with the foreach loop to printout the hashtable contents, but i've been coding for a good few hours straight and I can't think of the logic to printout my nested arraylist.
Help appreciated greatly.
Here is the code:
//Hashtable Declaration
static Hashtable sourceList = new Hashtable();
//Class For Storing Question Information
public class QuestionAnswerClass
{
public string simonQuestion;
public ArrayList simonAnswer = new ArrayList();
}
//Foreach loop which populates a hashtable with results from
//a linq query that i need to print out.
foreach (var v in linqQueryResult)
{
Debug.WriteLine(v.question);
newques.simonQuestion = v.question;
//Debug.WriteLine(v.qtype);
//newques.simonQType = v.qtype;
foreach (var s in v.solution)
{
Debug.WriteLine(s.Answer);
newques.simonAnswer.Add(s.Answer);
}
}
sourceList.Add(qTextInput,newques);
//foreach loop to print out contents of hashtable
foreach (string key in sourceList.Keys)
{
foreach(string value in sourceList.Values)
{
Debug.WriteLine(key);
Debug.WriteLine(sourceList.Values.ToString());
}
}
As you are using LINQ you are obviously not constrained to framework 1.1, so you should not be using the HashTable and ArrayList classes. You should use the strictly typed generic Dictionary and List classes instead.
You don't need a class to keep the question and answers in as you have the Dictionary. The class would only be an extra container with no real purpose.
//Dictionary declaration
static Dictionary<string, List<string>> sourceList = new Dictionary<string, List<string>>();
//Foreach loop which populates a Dictionary with results from
//a linq query that i need to print out.
foreach (var v in linqQueryResult) {
List<string> answers = v.solution.Select(s => s.Answer).ToList();
sourceList.Add(v.question, answers);
}
//foreach loop to print out contents of Dictionary
foreach (KeyValuePair<string, List<string>> item in sourceList) {
Debug.WriteLine(item.Key);
foreach(string answer in item.Value) {
Debug.WriteLine(answer);
}
}
If you need the class for some other reason, that could look like below.
(Note that the question string is both referenced in the class and used as key in the dictionary, but the dictionary key isn't really used for anything in this code.)
//Class For Storing Question Information
public class QuestionAnswers {
public string Question { get; private set; }
public List<string> Answers { get; private set; }
public QuestionAnswers(string question, IEnumerable<string> answers) {
Question = question;
Answers = new List<string>(answers);
}
}
//Dictionary declaration
static Dictionary<string, QuestionAnswers> sourceList = new Dictionary<string, QuestionAnswers>();
//Foreach loop which populates a Dictionary with results from
//a linq query that i need to print out.
foreach (var v in linqQueryResult) {
QuestionAnswers qa = new QuestionAnswers(v.question, v.solution.Select(s => s.Answer));
sourceList.Add(qa.Question, qa);
}
//foreach loop to print out contents of Dictionary
foreach (QustionAnswers qa in sourceList.Values) {
Debug.WriteLine(qa.Question);
foreach(string answer in qa.Answers) {
Debug.WriteLine(answer);
}
}
Try this
foreach (DictionaryEntry entry in sourceList)
{
Debug.WriteLine(entry.Key);
foreach (object item in (ArrayList)entry.Value)
{
Debug.WriteLine(item.ToString());
}
}
Minor tweaks
foreach (string key in sourceList.Keys)
{
Console.WriteLine(key);
foreach(string value in sourceList[key])
{
Console.WriteLine("\t{0}", value); // tab in answers one level
}
Console.WriteLine(); // separator between each set of q-n-a
}
Shouldn't this:
Debug.WriteLine(sourceList.Values.ToString());
be this?
foreach(var obj in sourceList.Values)
Debug.WriteLine(obj);
First, a strongly typed generic collection would make it easier. Let's start by defining an alias for the strongly typed collection:
using MyHash = System.Collections.Generic.Dictionary<string,
System.Collections.Generic.List<string>>;
From now on, MyHash means the same as the lengthy generic definition. Now you can declare the hashtable member like:
static MyHash sourceList = new MyHash();
And iterate over it like:
foreach (var pair in sourceList)
{
var question = pair.Value;
Console.WriteLine(question);
foreach (var answer in pair.Value)
Console.WriteLine(" " + answer);
}
Hope this is useful.
foreach (DictionaryEntry entry in Hashtable)
{
}
Find more in http://www.dotnetperls.com/hashtable

Categories