How do you add a array of strings to a List?
string csv = "one,two,three";
string[] parts = csv.Split(',');
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = new List<string>(new string[] { parts })
});
This works:
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = new List<string>(new string[] { "one", "two", "three" })
});
But then this is hardcoded. Is it even possible to split a string by "," and then add those values to a List
Use the ToList() method to convert the Array to a List.
string csv = "one,two,three";
string[] parts = csv.Split(',');
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = parts.ToList()
});
Well, parts is an array already, just pass it to the List's constructor:
string csv = "one,two,three";
string[] parts = csv.Split(',');
_MyList.Add(new ListObjects()
{
Name = tag.Name,
MyObjectList = new List<string>(parts)
});
You can just use ToList<TSource>() method to do this:
var List = csv.Split(',').ToList();
The easiest thing to do is simply to use string.split, followed by .ToList(), like so:
string csv = "one,two,three";
List<string> Strings = csv.Split(',').ToList();
Related
I'am comparing 2 List<string> wether the first list does contain an string that is not on the second List
This code works fine:
var onlyInXML = xmlList[i].columns.Except(rowAndTables[xmlList[i].table]);
if (onlyInXML.Any()) {
//Console.Write the not matching item here
}
I want to get the string, which is not matching. how do i do that ?
This way:
List<string> listOfStrings1 = new List<string>() { "abc", "def", "ghi", "lmn" };
List<string> listOfStrings2 = new List<string>() { "abc", "def", "lmn" };
List<string> listOfNotContainedStrings = listOfStrings1.Where(x => listOfStrings2.Contains(x) == false).ToList();
I'm learing the LINQ.
I made some codes.
string mySource = #"
#16
100%
Monitor
#19
98%
Guide
#77
0%
Cord
";
var myPattern = #"#(\d+)\r\n(\d+)%\r\n([^\r\n]*)\r\n";
var myCollection = Regex.Matches(mySource, myPattern, RegexOptions.Singleline)
.Cast<Match>().ToList();
MessageBox.Show(string.Join("\n", myCollection));
Looks nice.
But, What I really want to do is like this.
(this kind of data structure)
var myList = new List<string[]>();
var myArray1 = new string[] { "#16", "100%", "Monitor" };
var myArray2 = new string[] { "#19", "98%", "Guide" };
var myArray3 = new string[] { "#77", "0%", "Cord" };
myList.Add(myArray1);
myList.Add(myArray2);
myList.Add(myArray3);
What do I have to do ?
Regards
You can do:
List<string[]> myList = myCollection.Select(r => r.Value
.Split(new [] { '\r', '\n'},
StringSplitOptions.RemoveEmptyEntries)
).ToList();
You will end up with a List<T> with three elements, and each element would consists of array of strings.
I have an array of string :
string[] PropertyIds= new string[5];
A List of Class(Property)
List<Property> properties = new List<Property>();
The class Property has following fields:
PropertyId (string) and PropertyDesc (string)
I have to find all the values of PropertyId in array PropertyIds, which are not in List properties.
e.g.
string[] PropertyIds= new string[] { "one", "two", "three" };
List<Property> properties = new List<Property>()
{
new Property("one","This is p1"),
new Property("Five","This is p5"),
new Property("six","This is p6"),
};
Then my result should be two and three.
Use Enumerable.Except to get difference from two sequences:
var result = PropertyIds.Except(properties.Select(p => p.PropertyId));
I'm able to add List<string> in List<List<string>> array in this way:
List<string> first = new List<string> { "one", "two", "three" };
List<string> second = new List<string> { "four", "five", "six" };
List<List<string>> list_array = new List<List<string>> { first, second };
Now I need to create several lists populated with database records and then to add this lists to List<List<string>> array:
List<List<string>> array_list;
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
//Here I need to add temp_list to array_list
}
Create an empty array_list:
List<List<string>> array_list = new List<List<string>>();
Then use Add method to add items:
array_list.Add(temp_list);
Change your variable declaration to initialize an empty List:
List<List<string>> array_list = new List<List<string>>();
Then, just call .Add();
List<string> temp_list = new List<string> { one, two };
//Here I need to add temp_list to array_list
array_list.Add(temp_list);
Unless i'm reading this wrong, you should just be able to do:
array_list.add(temp_list);
This should work:
array_list.Add(temp_list);
List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
array_list.add(temp_list)
}
List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
array_list.Add(temp_list);
}
you can add directly;
array_list.Add(temp_list);
You have to alweys remeber to make new temp_list, don't use temp_list.clear(), like I did in my project =_=.
Blockquote
List<List<string>> array_list = new List<List<string>>();
while (dr.Read())
{
string one = dr["Row1"].ToString();
string two = dr["Row2"].ToString();
List<string> temp_list = new List<string> { one, two };
array_list.Add(temp_list);
}
Blockquote
I'm reading a local csv file which has data and I will eventually use to load into a database. My question is simple in nature but I can't seem to grasp the concept. Below is my code. Pretty simple stuff.
static void loadTables() {
int size = new int();
string[] empid = new string[0];
//string[] empid = new string();
List<string[]> EmployeeName = new List<string[]>();
List<string[]> EmployeeId = new List<string[]>();
List<string[]> Group = new List<string[]>();
List<string[]> Org = new List<string[]>();
List<string[]> Fund = new List<string[]>();
try {
using (StreamReader readFile = new StreamReader("C:\\temp\\groupFundOrgReport.csv"))
{
string line;
string[] row;
size = 0;
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(',');
/*resize the array up by 1*/
Array.Resize(ref empid,empid.Length+1);
/*I'm using size as a counter to add to the slot on the array.*/
empid[size] = row[0].Remove(0,1);
// here I receive error (the best overload match of system generic list?...etc)
EmployeeName.Add(row[0]);
size++;
}
}
}
catch(Exception e){
Console.WriteLine(e);
}
}
I have a list of string but any attempts to add a string to it gets me an error. In other words if I try to do this EmployeeName.Add(row[0].ToString); I get an error. However if I comment the line out I can use an old fashion array. I really like to use a list but I can't seem to be able to pass the value that I want. Can someone please point me in the right direction?
I guess from your code that the employee name is the first field of the CSV file.
You have declared EmployeeName as a List of arrays of strings List<string[]>, not as a list of strings List<string>.
Row[0] is the first string in an array, so you are trying to add a string to a list that is expecting you to add an array of strings.
You should just declare EmployeeName as a List<string>, using a line like:
List<string> EmployeeName = new List<string>();
or
var EmployeeName = new List<string>();
Your problem is the declaration of EmployeeName, it is a List of string arrays:
List<string[]> EmployeeName = new List<string[]>();
Change it to:
var EmployeeName = new List<string>();
Or, use the List<string[]> accordingly ...
EmployeeName is a List<string[]> so you have to write:
EmployeeName.Add(row);
To remove empty entries while splitting a string use:
row=line.Split(New String() {","},
StringSplitOptions.RemoveEmptyEntries);
All of them are List's of String Array's
List<string[]> EmployeeName = new List<string[]>();
List<string[]> EmployeeId = new List<string[]>();
List<string[]> Group = new List<string[]>();
List<string[]> Org = new List<string[]>();
List<string[]> Fund = new List<string[]>();
Only variable you can add would be like
//Define array of strings
var strArr = new string[] {"abc", "xyz"};
then you can call
EmployeeName.Add(strArr);
although changing the List generic type to string type will solve your problem
List<string> EmployeeName = new List<string>();
List<string> EmployeeId = new List<string>();
List<string> Group = new List<string>();
List<string> Org = new List<string>();
List<string> Fund = new List<string>();
var str = "My String";
EmployeeName.Add(str);