I have a Dictionary type variable where I'm keeping "string" type key and "List " type values.
The problem is, in case of loop all the previous values are getting replaced by the last value.
Why this happens?
List<IWebElement> indicationsElement = ReturnIndicationList();
drugsDB = new List<string>();
for (int i = 0; i < indicationsElement.Count;i++ )
{
string key = indicationsElement[i].Text.ToString();
dt = ZittarPatt.getDrugsByIndication(ClientName, key);
drugsDB.Clear();
for (int k = 0; k < dt.Rows.Count; k++)
{
drugsDB.Add(dt.Rows[k].ItemArray[3].ToString().Trim());
}
drugsByindicationDictionary.Add(key, drugsDB);
}
You're adding the same reference every iteration instead of adding new instance of List<string>.
Every time you use .Clear it clears all the entries at drugsByindicationDictionary which are already the same entry.
Thus, only the last addition to drugsDB will be saved. (No .Clear is used at the end)
You should do the following code:
List<IWebElement> indicationsElement = ReturnIndicationList();
for (int i = 0; i < indicationsElement.Count;i++ )
{
string key = indicationsElement[i].Text.ToString();
dt = ZittarPatt.getDrugsByIndication(ClientName, key);
var drugsDB = new List<string>();
for (int k = 0; k < dt.Rows.Count; k++)
{
drugsDB.Add(dt.Rows[k].ItemArray[3].ToString().Trim());
}
drugsByindicationDictionary.Add(key, drugsDB);
}
Change drugsDB.Clear(); to drugsDB = new List<string>();
Related
My Code:
DataTable dt = fs.DataTable;
int columnsNumber = dt.Columns.Count;
string[] array = new string[columnsNumber];
for (int k=0; k==columnsNumber; k++)
{
array[k] = dt.Columns[k].ColumnName;
}
foreach (var item in array)
{
MessageBox.Show(item);
}
MessageBox has displaying the blank message.
If I have run this code, no problem;
array[1] = dt.Columns[1].ColumnName;
array[2] = dt.Columns[2].ColumnName;
array[3] = dt.Columns[3].ColumnName;
This work. What is the problem ?
You have included a == operator in for loop where you should be using <
Change
for (int k=0; k==kolonSayisi; k++)
to
for (int k=0; k<kolonSayisi; k++)
Your cicle just checking k==kolonSayisi:
for (int k=0; k==kolonSayisi; k++)
{
array[k] = dt.Columns[k].ColumnName;
}
I think you should write it like this:
for (int k=0; k < columnsNumber; k++)
{
array[k] = dt.Columns[k].ColumnName;
}
You can also use this way
var listToArray = new listToArray<string>();
foreach (DataColumn dataCol in dt.Columns)
listToArray.Add(dataCol.ColumnName);
listToArray.ToArray();
Hope it helps.
A for loop works the following way:
In the parentheses the first part defines a counting or increment variable and sets the starting value. The second part is the cancel condition. You can read it like: if condition is false then stop the loop. The third part defines the step size, how you want to increment your variable.
Now if you look at your condition k==columnsNumber you are trying to check if k equals the number. In the first iteration where k is 0 it will return false if columnsNumber is not 0. So your loop will stop.
You can use like this:
DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Columns.Add("col3");
var array = table.Columns
.Cast<DataColumn>()
.Select(c => c.ColumnName)
.ToArray();
foreach(var item in array)
{
MessageBox.Show(item);
}
DataTable users_table = db.GetRows("SELECT * FROM users");
int rowCount = users_table.Rows.Count;
int columnCount = users_table.Columns.Count;
Dictionary<int, Dictionary<string, string>> baskets = new Dictionary<int, Dictionary<string, string>>();
for (int i = 0; i < rowCount; i++)
{
baskets.Add(i, new Dictionary<string, string>());
for (int j = 0; j < columnCount; j++)
{
var colName = (users_table.Columns[i].ColumnName).ToString();
var colValue = users_table.Rows[i][j].ToString();
baskets[i].Add(colName, colValue);
Response.Write(baskets[i]["user_id"].ToString());
}
}
This is the error i got.
ArrgumentException was unhandled by user code at baskets[i].Add(colName, colValue);
An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code
Additional information: An item with the same key has already been added.
What I'm doing wrong? I debug it and value of "i", "colName" and "colValue" is correct. Don't know why it's giving me error.
I'm newbie in .net programming
In your inner loop i does not change and therefore the value of colName does not change. As a result you're adding the same key each time through:
var colName = (users_table.Columns[i].ColumnName).ToString();
baskets[i].Add(colName, colValue);
Most likely you meant to grab the name from Columns[j] instead of Columns[i] and this is a "second pair of eyes" problem.
FWIW, you can avoid this by using more descriptive counter variable names instead of the "traditional" i and j. Your code from the question translates to, which IMHO is easier to notice as "something's wrong with Columns[row]":
for (int row = 0; i < rowCount; row++)
{
baskets.Add(row, new Dictionary<string, string>());
for (int col = 0; col < columnCount; col++)
{
var colName = (users_table.Columns[row].ColumnName).ToString();
var colValue = users_table.Rows[row][col].ToString();
baskets[row].Add(colName, colValue);
Response.Write(baskets[row]["user_id"].ToString());
}
}
Side note 1: DataTable.ColumnName is already a string, so your call to ToString() above is unnecessary.
Side note 2: Unless the "user_id" column is column 0, the Response.Write line is going to fail. Was this perhaps intended to be outside of the inner loop, or maybe just added for debugging purposes?
Side note 3: You can also keep a reference to the inner dictionary instead of using the indexer baskets[row] each time through the inner loop. Yes, this probably is a difference of microseconds, but:
for (int row = 0; i < rowCount; row++)
{
var colDic = new Dictionary<string, string>();
baskets.Add(row, colDic);
for (int col = 0; col < columnCount; col++)
{
var colName = users_table.Columns[row].ColumnName;
var colValue = users_table.Rows[row][col].ToString();
colDic.Add(colName, colValue);
}
}
Just remove the line
baskets.Add(i, new Dictionary<string, string>());
Because this adds an integer to the Dictionary that is 0 in the first iteration and then when it reaches to baskets[i].Add(colName, colValue); the dictionary has already added 0 in the key and would not be able to add the same item again. So removing that line will do make your code something like this
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
var colName = users_table.Columns[j].ColumnName;
var colValue = users_table.Rows[i][j].ToString();
baskets[i].Add(colName, colValue);
Response.Write(baskets[i]["user_id"].ToString());
}
}
might do the trick for you
I have the two list
nested list of string, and
list in string
In index list, I want to add linesOfContentwith a common value and in-between i want to add separate string ":".
For that i write a code, but, I face a problem "cannot convert from 'string' to 'system.collections.generic.list string'". How to solve this.
int common = 10;
List<List<string>> index = new List<List<string>>();
List<int> linesOfContent = new List<int>();
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
linesOfContent.Add(i+":"+common);
}
index.Add(linesOfContent);
}
Expected Output:
index[0][0] = 0:10
index[0][1] = 1:10
index[0][2] = 2:10
...
...
A List of Lists of string should contain Lists of string, not Lists of int.
int common = 10;
List<List<string>> index = new List<List<string>>();
List<string> linesOfContent = new List<string>();
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
linesOfContent.Add(i.ToString() +":"+common.ToString());
}
index.Add(linesOfContent);
}
Each item in your index list is a List<string>. When you try to add an item, it should be a List. However, you're trying to add a string to it, linesOfContent+":"+common is considered a string.
Solution:
Linq's Select Method (aka Projection) can be used to transform each element inside a sequence:
index.Add(linesOfContent.Select(x=> x.ToString() + ":" + common).ToList());
Be aware that the way you're constructing your loops results in some duplicate records.
Here's the code, without the foreach loops, instead it uses Enumerable.Range:
linesOfContent.AddRange(Enumerable.Range(0, 5).Select(i => i.ToString() + ":" + common.ToString()).ToArray());
index.Add(linesOfContent);
I'm on VS Windows Forms Application and I'm NOT using any other form of coding (e.g. linq) - just the basic style of coding which goes like this;
List<string> brandList = new List<string>();
//brand items are added to the list
//go through list
for(int i = 0; brandList.Count; i++)
{
if(brandList[i]== "BrandName1")
{
//count for that brandName
}
}
What I want to know is how to I get the count for how many times a brand has occurred in the list?
This code will not need to be case sensitive either because it is being read in from a file..
If you don't want/can't use LINQ you could use a Dictionary<string, int>:
Dictionary<string, int> dict = new Dictionary<string, int>();
for(int i = 0; brandList.Count; i++)
{
string brand = brandList[i];
int count = 1;
if(dict.ContainsKey(brand))
count = dict[brand] + 1;
dict[brand] = count;
}
Now you have all brands as key and their counts as value.
I really don't see the problem because your code is already have the solution if I understand correctly the question. You go through the elements, if the current element is the element to count you increment a variable.
const string BRAND = "BrandName1";
int count = 0;
foreach (string brand in brandList)
{
if (string.Compare(brand, BRAND, true))
{
count++;
}
}
Obviously you can use for (int i = 0; i < brandList.Count; i++) and brandList[i] instead of foreach, but it's more like C#.
How about this:
List<string> brandList = new List<string>();
//brand items are added to the list
//sort list to get number of occurences of each entry
brandList.Sort();
var occurences = new List<KeyValuePair<string, int>>();
//go through list
var numBrand = 1;
for (int i = 0; i < brandList.Count-1; i++)
{
if (string.Equals(brandList[i + 1], brandList[i]))
{
numBrand++;
}
else
{
occurences.Add(new KeyValuePair<string, int>(brandList[i], numBrand));
numBrand = 1;
}
}
var highestCount = occurences.OrderBy(o => o.Value).ToList()[0].Key;
It might skip the last entry if that is a single occurences but then that's not the highest anyway.
Would that do the trick for you?
I am trying to having a new list added on every for loop iteration. I have the following code:
for (int i = 0; i < torni.Length; i++)
{
List<string> torni+i = // STUCK HER
}
Listnames should be like torni0, torni1, torni2 ......
Would really appreciate your assistance. Thanks!!
One way of doing this that would be slightly different would be to make a list of lists and then each index would be a discrete list.
You can't dynamically generate variable names in c#
like this:
tornis = new List<List<String>>()
for (int i = 0; i < torni.Length; i++)
{
tornis.append(new List<String>())
}
Alternatively as DanH Points out a dictionary of lists
tornis = new Dictionary<String,List<String>()
for (int i = 0; i < torni.Length; i++)
{
tornis.add("torni" + i, new List<String>())
}
This will give you a dictionary with the keys of the convention you want and a list of lists.
If you need each list only for the duration of a single loop iteration, then you don't need different list names:
for (int i = 0; i < torni.Length; i++)
{
List<string> temporaryList = new List<string>();
// use the list here
}