example...
List<array> thisListOfArray = new List<array>();
List<string> thisArrayA= new List<string>();
List<string> gMaintenanceB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
thisArrayB.Add("ItemB1");
thisArrayB.Add("ItemB2");
thisArrayB.Add("ItemB3");
thisListOfArray.Add(thisArrayA.ToArray());
thisListOfArray.Add(thisArrayB.ToArray());
I want to get every value that I have inputted inside the thisListOfArray.
you can get it following way. here is the code
List<string[]> thisListOfArray = new List<string[]>();
List<string> thisArrayA = new List<string>();
List<string> thisArrayB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
thisArrayB.Add("ItemB1");
thisArrayB.Add("ItemB2");
thisArrayB.Add("ItemB3");
thisListOfArray.Add(thisArrayA.ToArray());
thisListOfArray.Add(thisArrayB.ToArray());
List<string> lstNewstring = new List<string>();
foreach (var strArray in thisListOfArray)
{
foreach (var str in strArray)
{
lstNewstring.Add(str);
}
}
MessageBox.Show(lstNewstring.Count.ToString());
Change your code like this:
List<List<string>> thisListOfArray = new List<List<string>>();
List<string> thisArrayA = new List<string>();
List<string> gMaintenanceB = new List<string>();
thisArrayA.Add("ItemA1");
thisArrayA.Add("ItemA2");
thisArrayA.Add("ItemA3");
gMaintenanceB.Add("ItemB1");
gMaintenanceB.Add("ItemB2");
gMaintenanceB.Add("ItemB3");
thisListOfArray.Add(thisArrayA);
thisListOfArray.Add(gMaintenanceB);
foreach (var itm in thisListOfArray.SelectMany(item => item))
{
MessageBox.Show(itm);
}
If you have:
List<string[]> listOfArray
string[] array
Add the array to the List with listOfArray.Add(array);
Not sure where you'll be getting array from.
Related
I need an overview of two floors as a treeview. I have a csv, but don't know how to get it into the treeview. I saw many examples but nothing works. Thanks.
That's how my csv looks like:
floor;room;place;pcnumb
4;1;1;6001
4;1;2;6002
4;1;3;6003
4;1;4;6004
...
5;8;82;5082
5;8;83;5027
5;8;84;5084
5;9;85;5028
var reader = new StreamReader(File.OpenRead(#"csvh.csv"));
List<string> listA = new List<string>();
List<string> listB = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(';');
listA.Add(values[0]);
listB.Add(values[1]);
foreach (var column1 in listA)
{
Console.WriteLine(column1);
}
foreach (var column2 in listA)
{
Console.WriteLine(column2);
}
}
That's what I tried, but it doesn't work with my csv
I have 5 datasets coming from Database and have saved them in List like below:
var list1 = new List<Class1>
var list2 = new List<Class2>
var list3 = new List<Class3>
var list4 = new List<Class4>
var list5 = new List<Class5>
I need to convert all 5 lists into a csv file (excel type).
I can do this only for lis1 dataset as of now.
How can we merge all list and print the data in CSV?
The format of the csv file is as follows
Year,Make,Model,Length
1997,Ford,E350,2.35
2000,Mercury,Cougar,2.38
The following code shows how to implement it or you can use ready-made libraries.
public void WriteToCSV()
{
var csv = new StringBuilder();
foreach (var item in list1)
{
string line = "field1,field2,...";
csv.AppendLine(line);
line = string.Format("{0},{1},...",item.field1,...);
csv.AppendLine(line);
}
//.........................................
//.........................................
foreach (var item in list5)
{
string line = "field1,field2,...";
csv.AppendLine(line);
line = string.Format("{0},{1},...",item.field1,...);
csv.AppendLine(line);
}
string fileName = #"D:\WriteText.csv";
if (File.Exists(fileName))
System.IO.File.AppendAllText(fileName, csv.ToString());
else
System.IO.File.WriteAllText(fileName, csv.ToString());
}
Is it possible to add a variable to a list name when using it?
Something like this:
string id = "1"; //Could be 2
List<string> List1 = new List<string> {"1","11" };
List<string> List2 = new List<string> {"2","22" };
foreach (var element in List+id)
{ //code here }
IDs could be a dozen different values, so I didn't even try with regular if(). Would that be the only way?
Use a dictionary:
var dict = new Dictionary<string, List<string>>();
dict.Add("1", new List<string> {"1","11" });
dict.Add("2", new List<string> {"2","22" });
Then you can do
foreach (var element in dict[id])
{
}
I want a method to return a list which contains two more list which are having two different data types, like :
List<List<object>> parentList = new List<List<object>>();
List<string> childList1 = new List<string>();
List<DataRow> childList2 = new List<DataRow>();
parentList.Add(childList1);
parentList.Add(childList2);
return parentList;
As per above code I am getting an error
The best overloaded method match for 'System.Collections.Generic.List>.Add(System.Collections.Generic.List)' has some invalid arguments
Please can anyone suggest me the best approach to handle this.
Thanks
What about creating an object of your class as like this?
public class myParent
{
public List<string> childList1 = new List<string>();
public List<DataRow> childList2 = new List<DataRow>();
}
public void someFun()
{
List<myParent> parentList = new List<myParent>();
myParent myParentObject = new myParent();
myParentObject.childList1 = new List<string>() { "sample" };
myParentObject.childList2 = new List<DataRow>() { };
parentList.Add(myParentObject);
}
I'm not sure why would you like to mix objects like this, but you could use ArrayList for this. Refer example below:
List<ArrayList> data = new List<ArrayList>();
data.Add(new ArrayList(){12, "12"}); //Added number and string in ArrayList
data.Add(new ArrayList() {"12", new object() }); //Added string and object in ArrayList
Update
In your case the using the array list like below could be better
var data = new ArrayList();
data.Add(new List<object>());
data.Add(new List<string>());
data.Add(new List<int>());
Using LINQ, how I can retrieve list of list of item where match with another list?
Example:
List<string> data = new List<string>();
List<List<string>> datas = new List<List<string>>();
List<string> dataSearch = new List<string>();
dataSearch.Add("01");
dataSearch.Add("02");
data.Add("01");
data.Add("Book");
data.Add("9");
datas.Add(data);
data = new List<string>();
data.Add("02");
data.Add("Pen");
data.Add("2");
datas.Add(data);
data = new List<string>();
data.Add("03");
data.Add("Pencil");
data.Add("5");
datas.Add(data);
Expected Result in List<List<string>>:
List<string> ("01", "Book", "9")
List<string> ("02", "Pen", "2")
I try
var result = datas.Where(x => x[0].Contains(dataSearch)).ToList();
I got red underline in editor.
This is what you are looking for:
List<string> data = new List<string>();
List<List<string>> datas = new List<List<string>>();
List<string> dataSearch = new List<string>();
dataSearch.Add("01");
dataSearch.Add("02");
// same as your code
var result = datas.Where(i => dataSearch.Contains(i[0])).ToList();
If the three items in the List<List<string>> has a closely bound relationship it is better to create a class for this and then make a list of these objects.
Class StationaryItem
{
int ItemCode;
String ItemName;
float Price;
public StationaryItem(int Code, string ItemName,float Price)
{
this.ItemCode = Code;
this.ItemName = ItemName;
this.Price = Price;
}
}
public static void Main ()
{
List<StationaryItem> ItemList = new List<StationaryItems>
ItemList.Add( new StationaryItem(01,"Pen",20));
ItemList.Add( new StationaryItem(02,"Pencil",5));
//After you make a class like this you can use Linq to manuplate easily
}