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);
Related
I have 2 T Lists e.g:
list1 with attributes {ID, Name, Email}
list2 is of the same type
What I want is to be able to replace list1 ID values with ID values from list2, not affecting any Name and Email values
Eventually I would be even happier if I could have List list2 values to replace ID values from list1.
Thanks a lot.
I have tryed using for loop but it does not seem to work:
`for (int i = 0; i < list1.Count; i++)
{
list1[i].GroupID =list2[i].GroupID;
}`
for (var i = 0; i < list1.Count; i++)
{
var temp = list1[i].GroupID;
// swap
list1[i].GroupID = list2[i].GroupID;
list2[i].GroupID = temp;
}
Or Use tuple.
for (var i = 0; i < list1.Count; i++)
{
// swap
(list1[i].GroupID, list2[i].GroupID) = (list2[i].GroupID, list1[i].GroupID)
}
Both methods must have the same count of list1 and list2.
Apologies if this is a duplicate question, but similar questions on retrieving duplicate value pair key values don't go in-depth enough for me to understand how to use it.
I'm currently using a dictionary of lists containing text and using lists.Intersect to compare the lists with each other. How would I go about getting the key value of which two lists have duplicates within them? Here's my code:
private string findDuplicates(Dictionary<int, List<string>> lists)
{
List<string> intersection = new List<string>();
int i;
int j;
for (i = 0; i < 13; i++)
{
for (j = i + 1; j < 13; j++)
{
intersection.Add(String.Join(" ", lists[i].Intersect(lists[j])).ToString());
};
//Attempt to retrieve value
if (intersection.Count > 0)
{
numberOfDomainsTB.Text += "\nLocations:\nList " + i + " and List " + j;
}
};
Other methods I see to do this for ordinary dictionaries is:
lists.GroupBy(x => x.Value).Where(x => x.Count() > 1);
But that doesn't compare the lists' internal values.
Thanks.
private string findDuplicates(Dictionary<int, List<string>> lists)
{
List<string> intersection = new List<string>();
int i;
int j;
string compare = "";
for (i = 0; i < lists.Count - 1; i++)
{
for (j = i + 1; j < lists.Count - 1; j++)
{
compare = String.Join(" ", lists[i].Intersect(lists[j])).ToString();
if (!String.IsNullOrEmpty(compare))
{
intersection.Add(String.Join(" ", lists[i].Intersect(lists[j])).ToString());
}
};
};
}
I fixed this by realizing that intersection.Add will always add a new value to the list, regardless of whether it is empty or not. As such, any comparison made would always show matches with every list. So I opted to do Intersect twice: once to store in a string which is then checked if null/empty, and if not then again in the intersection.Add line to actually add the intersecting values.
NOTE: I didn't need to use a dictionary in the end. It is simply a remnant of the previous attempt to carry out this task. Any ordinary list would work.
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>();
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
}
I am making a program that stores data in a 2D array. I would like to be able to delete rows from this array. I cannot figure out why this code doesn't work:
for (int n = index; n < a.GetUpperBound(1); ++n)
{
for (int i = 0; i < a.GetUpperBound(0); ++i)
{
a[i, n] = a[i, n + 1];
}
}
Could someone please help me out? I would like it to delete a single row and shuffle all the rows below it up one place. Thankyou!
you need to create a new array if you want to delete an item
try something like this
var arrayUpdated = new string[a.GetUpperBound(1)][a.GetUpperBound(0)-1];
for (int n = index; n < a.GetUpperBound(1); n++)
{
for (int i = 0; i < a.GetUpperBound(0); i++)
{
arrayUpdated [i, n] = a[i, 1];
}
}
The nested for loop method here works well: https://stackoverflow.com/a/8000574
Here's a method that converts the outer loop of the [,] array method above to use linq. Using linq here is only recommended if you are also doing other things with linq during the traversal.
public T[,] RemoveRow<T>(T[,] array2d, int rowToRemove)
{
var resultAsList = Enumerable
.Range(0, array2d.GetLength(0)) // select all the rows available
.Where(i => i != rowToRemove) // except for the one we don't want
.Select(i => // select the results as a string[]
{
T[] row = new T[array2d.GetLength(1)];
for (int column = 0; column < array2d.GetLength(1); column++)
{
row[column] = array2d[i, column];
}
return row;
}).ToList();
// convert List<string[]> to string[,].
return CreateRectangularArray(resultAsList); // CreateRectangularArray() can be copied from https://stackoverflow.com/a/9775057
}
used Enumerable.Range to iterate all rows as done in https://stackoverflow.com/a/18673845
Shouldn't ++i be i++? ++i increments before matrix operation is performed(ie pre-increment)