The dictionary <string, List <string>> has all the values ββthat are the same as for the last entered value
I have a dictionary of string as key and List as value.
And adding an entry as follows: -
allDDLs = new Dictionary<string, List<string>>(); List<string> temp; temp = ddlData.get("INV_DDL_Access_Mechanism", "Type"); allDDLs.Add("Access_Mechanism", temp); temp = ddlData.get("INV_DDL_Application_Operating_System", "OS_Name"); So there are 18 entries. But when I debugged the code, I found that all the values ββare the same as the values ββof the last entered value. But the keys are fine.
For example, when I get a value for "INV_DDL_Access_Mechanism", I get the same value as for "INV_DDL_Application_Operating_System".
I do not know what is wrong with him. Any help would be greatly appreciated. :-)
Thanks in advance!
EDIT 1: Code for accessing dictionary values:
List<string> listOfColumns = allDDLs.Keys.ToList<string>(); for(int i = 0; i < listOfColumns.Count(); ++i) { string columnName = listOfColumns[i].ToString(); List<string> tempDDL = new List<string>(); tempDDL = allDDLs[columnName]; List<string> columnValues = DataColumnToList(columnName, excelDataSet.Tables[0]); for (int j = 0; j < columnValues.Count(); ++j ) { if (!tempDDL.Contains(columnValues[j])) { errorReport.Rows[j][columnName.ToString()] = columnValues[j].ToString() + "LOLOLOL"; } } } Edit 2: Get method code.
public List<string> get(string tableName, string parameter) { ds.Clear(); valuesForDDL.Clear(); ds = objDatabase.ByText("Select distinct " + parameter + " from " + tableName + " where Del_Status = 'Available'"); foreach (DataRow row in ds.Tables[0].Rows) { valuesForDDL.Add(row.ItemArray[0].ToString()); } return valuesForDDL; } The problem is with your get method.
Each time you call get you return valuesForDDL , and then the next time you clear it and populate it again. Error not found yet?
valuesForDDL should NOT be a field. It is like a global state, and a global state is killing. Make it local and your code will work.
public List<string> get(string tableName, string parameter) { var valuesForDDL = new List<Whatever>(); var ds = objDatabase.ByText("Select distinct " + parameter + " from " + tableName + " where Del_Status = 'Available'"); foreach (DataRow row in ds.Tables[0].Rows) { valuesForDDL.Add(row.ItemArray[0].ToString()); } return valuesForDDL; } EDIT:
Orthogonal note. With LINQ, you can make this code much more enjoyable. I will reorganize your code as follows:
public IEnumerable<string> Get(string tableName, string parameter) { var ds = objDatabase.ByText("Select distinct " + parameter + " from " + tableName + " where Del_Status = 'Available'"); return ds.Tables[0].Rows.Select(row => row.ItemArray[0].ToString()); } You said in your comment:
I clear the list in this code before adding new elements to it. Then return the list.
This may be a problem ... if you return the same List<string> in every ddlData.get() call (using an instance / static field or property), and you just execute List<string>.Clear() , you overwrite the contents of the last returned list.
In ddlData.get() you should make a new List<string>() instead of Clear() existing list.
If this is not your problem, send more code (especially the ddlData.get () method or the whole class).
You are using the same List instance ("temp" in your code).
Create new
List<string> for each dllData.get(".... , and everything will be fine.
List<string> temp1 = ddlData.get("INV_DDL_Access_Mechanism", "Type"); List<string> temp2 = ddlData.get("INV_DDL_Application_Operating_System", "OS_Name"); .... allDDLs.Add("Access_Mechanism", temp1); allDDLs.Add("Application_Operating_System", temp2); EDIT
You would prefer not to use temporary variables, so use:
allDDLs.Add("Access_Mechanism", ddlData.get("INV_DDL_Access_Mechanism", "Type")); allDDLs.Add("Application_Operating_System", ddlData.get("INV_DDL_Application_Operating_System", "OS_Name")); If ddlData.get(...) creates a new List<string> each time, it will work for sure.