add two strings to two dimensional list - c#

i want to add in two set of strings to one list
i receive error that i cant use add
List<List<String>> lastmodified1 = new List<List<String>>();
lastmodified1.Add(new List<String>());
foreach (string filenamelocal in files)
{
string name = Path.GetFileName(filenamelocal);
lastmodified1[0][1].Add(Convert.ToString(filenamelocal));
lastmodified1[0][0].Add(Convert.ToString(File.GetLastAccessTime(filenamelocal)));
}

you target the list with the first index
lastmodified1[0].Add(Convert.ToString(filenamelocal));
lastmodified1[0].Add(Convert.ToString(File.GetLastAccessTime(filenamelocal)));
with the index [0] you are target the list, because the first List is implicit.
so with
lastmodified1[0].Add(Convert.ToString(filenamelocal)); //this is accessing to the List inside the First list
you are accesing to the second List inside from the first List.
with the second index you're accessing to the value of the second list, in this case String, and you can get the method add because strings dont have
lastmodified1[0][0] //this access to string value

Related

C# How to prevent ListBox to select first item when you unselect the last one

C# How to prevent ListBox in MultiSimple selection mode to select first item automatically, when you unselect the last one selected item in the box - it happens only if listbox represented by my own class objects, and everything is ok when it represented by string objects. Thnx!
It seems like keeping track of the order of the list is the most important part. I would suggest maybe making an array of a struct. You can make this struct contain whatever you want for example:
struct itemList
{
public string itemName;
public int itemIndex;
//include whatever other variables that you need included in here.
}
Also, make sure you place your struct before the namespace. Then, making the array of the struct would look like this:
itemList[] items1 = new itemList[listBoxName.SelectedItems.Count];
All you would have to do then is to add the items to the array before you reorder the listBox
for (int i = 0; i < listBoxName.SelectedItems.Count; i++)
{
items1[i].itemName = listBoxName.SelectedItems[i].ToString();
items1[i].itemIndex = listBoxName.SelectedIndices[i];
}
Thank you very much, but i already use some like this. I don't understand why first item of listBox selected everytime i assign preloaded list as DataSource. I resolve this problem, by making another one temporal List of my object class, which items readed from binary file, and then push them one by one to my original List in foreach cycle by listOfObjects.Add(object) method. I know that every time after code ListOfTags.DataSource = null;
ListOfTags.DataSource = tags;
ListOfTags.DisplayMember = "Name"; if my tags (it is a List) are preloaded even in code (for example if i write code List<Tag> tags = new List<Tag> {new Tag("1"),new Tag("2"), new Tag("3")}; , this takes situation when first item of listbox selected, and starts selects everytime after that when i try do deselect last selected item in listBox.

C# Add Labels to an ArrayList and take Sums with conditions

I have a List of Arrays and want to take sums from specific fields of the Arrays.
I declare the List like that:
List<Object[]> fieldsFM = new List<Object[]>();
Then I take some fields of existing (string)Arrays and insert them into my List changing their type, like that:
foreach (var item in files) {
string text = folder + item.ToString();
string[] fields = fileToArray(text);
object[] tempFields = {fields[0],fields[2],DateTime.Parse(fields[4].Substring(6, 2)+"-"+fields[4].Substring(4, 2)+"-"+fields[4].Substring(0, 4)),double.Parse(fields[11], CultureInfo.InvariantCulture),double.Parse(fields[12], CultureInfo.InvariantCulture),double.Parse(fields[13], CultureInfo.InvariantCulture),double.Parse(fields[14], CultureInfo.InvariantCulture),double.Parse(fields[15], CultureInfo.InvariantCulture),double.Parse(fields[16], CultureInfo.InvariantCulture),double.Parse(fields[17], CultureInfo.InvariantCulture),double.Parse(fields[18], CultureInfo.InvariantCulture),double.Parse(fields[19], CultureInfo.InvariantCulture),double.Parse(fields[20], CultureInfo.InvariantCulture)};
fieldsFM.Add(tempFields);
}
On field[4] I have some dates, so I want to take sums of fields[11]-[20] by date, that means I have to group by date.
I want to ask how I can add labels to my list so I can take the sums.
Update
The data structure is an array of strings, for example:
[abc,abc,20170616,3.1,3.2,3.3]
I create a list and add in it those Arrays (but first change the type of the fields) In 3rd field of each Array I have dates and in other fields I have values. I want to take the sums of the values but group by date.
It was suggested in comments that I utilize KeyPairValue. If I use a KeyPairValue what do I have to change on the call to the .Add method?

Why am I getting ArgumentOutOfRangeException in Lists?

I am using List of Lists in my project. When i run program i get ArgumentOutOfRangeException. But there is no range specified in list.
I declared list like this:
public static List<List<string>> list = new List<List<string>>();
Now i want to add my name in the first "list" which is in the List of lists.
list[0].Add("Hussam"); //Here i get ArgumentOutOfRange Exception.
What should I do now?
But there is no range specified in list
No, there's an index specified (as an argument), and that's what's out of range. Look at your code:
list[0].Add("Hussam");
That's trying to use the first list in list - but is list is empty, there is no first element. The range of valid arguments to the indexer is empty, basically.
So first you want:
list.Add(new List<string>());
Now list[0] will correctly refer to your empty List<string>, so you can add "Hussam" to it.
You want to add an item to the first item in an empty list... That isn't going to work. First, add the list inside the other list:
public static List<List<string>> list = new List<List<string>>();
List<string> innerList = new List<string>();
list.Add(innerList);
innerList.Add("Hussam");
Why are you creating a list of a list? Wouldn't List suffice? What is happening here is the inner list is not being initialized.
list.Add(new List<string>());
list[0].Add("Jimmy");
In this case ocurred an exception because you tried acess an index which not exists, then you must add an inner initial list, which could be done follows:
list.Add(new new List<string>());
Or, if you want add an first name directly:
list.Add(new new List<string>(){"Hussam"});
Ok so first, you have to understand that the "index" only comes after the value has been declared. Lists behave different. They are not like arrays. You get the index in which you want to store the item and when you do that, you use the code array[index] = value;.
But in a List, to give a value to a completely new item, you use the method Add(value).
So here's a reminder: Systems.Collections.Generic.List<> has nothing to do with array[ ]s
You cannot access list[0] as there is no item at index 0. The list is empty.
You need to add a new List like this:
list.Add(new List<string> { "Hussam" });
or, assign a list to index 0 and then add to it as per your posted code:
list.Add(new List<string>());
list[0].Add("Hussam");
If you don't always know if the list will be be empty or not you can use FirstOrDefault (a LINQ method) to check if there is any entry at index 0 and assign one if not, otherwise use the existing inner list:
var innerList = list.FirstOrDefault();
if (innerList == null)
{
innerList = new List<string>();
list.Add(innerList);
}
innerList.Add("Hussam");
The problem is, your nested list hasn't been initialized, with anything.
So, calling the first item of the nested list is correctly telling you there is nothing in it.
To verify:
int superlistCounter = 1;
int sublistCounter = 1;
foreach(var sublist in list)
{
Console.WriteLine("Now in List #" + superlistCounter);
foreach(var item in sublist)
{
Console.WriteLine("List item #" + sublistCounter + ": " + item)
}
}
The output will be:
Now in List #1
It sounds like you're expecting:
Now in List #1
List Item #1: Hussam
To fix this, simply initialize your list!
public static List<List<string>> list = new List<List<string>>();
// ...
List<string> subList1 = new List<string>();
list.Add(subList1);
subList1.Add("Hussam");

Linq query to filter id inside a list of list c#

I have a list of result List where it contains List inside of it.I have another list where it contains List alone.I want to filter using a linq query from the data where it should return all the data which contains skill id from the second list.
var list = this._viewModel.Data.Select(T => T.SkillsList);
var filtered = item.Skills.Contains(list.Where(t=>t.ToString()).ToList();
from the first list it contains List of decimals inside the skill list;
item.Skills contains list where the fields are skillid and code.
item is another object which contains the skillslist.
if skillId is a variable and assuming that SkillsList contains a property called Id. Then the following would work in getting you any data that has the specified skillId.
var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=>s.Id == skillId));
If Skillslist is just an array of integers then the following would work.
var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=> s == skillId));
Now if you are checking against a list the following would work.
var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=> skillsList.contains(s));

Add element to list before specific element

I have a list of items, lets say 100 items. I need to add another element before the existing element that matches my condition. What is the fastest way and the most performance optimized to do this?
ie.:
foreach (var i in myList)
{
if (myList[i].value == "myValue")
{
myList[i-1] add ("someOtherValue")
}
}
Maybe i should use other container?
First you could find the index of your item using FindIndex method:
var index = myList.FindIndex(x => x.value == "myvalue");
Then Insert at the right point:
myList.Insert(index,newItem);
Note that inserting at a given index pushes everything else forward (think about finding your item at index 0).
Consider using a LinkedList<T>. It has the advantage that inserting or removing items does not require shifting any items. The disadvantage is that items cannot be accessed randomly. You have to traverse the list starting at the first or last item in order to access the items.
myList.Insert(myList.IndexOf("myValue") - 1, "someOtherValue");
You should probably check to make sure myvalue exists first, and it is not in index 0.
int index = myList.IndexOf("myValue");
if (index >= 0)
myList.Insert(index, "myNewValue");
By the way, you should not modify your own collection or list while iterating with for-each (as in your code above).
I presume the list is an array - in which case have you tried doing this with Linq?
string[] mylist = new string[100];
// init the list
List<string> list = keys.ToList();
list.Insert(1,"somethingelse");
mylist = list.ToArray(); // convert back to array if required
if it is a List to begin with, you can skip the conversions and use Insert directly.

Categories