问题描述:

i am trying create one list item as another list.i need that list every item has one another list.i created string as list for one loop. and next loop i needs each item as one list to insert data based on range.

how to do this..??

my code:

List<string> l = new List<string>(s.Split(';'));

foreach (string item in l)

{

if (item.Contains("-"))

{

List<string> parts = new List<string>();

int min = Int32.Parse(parts[0]);

int max = Int32.Parse(parts[1]);

for (int m = min; m <= max; m++)

{

// add the value in i to the data

cmd = "insert into quickcom values('" + destination + "','" + codes + "','" + prefix + "','" + rate + "','" + level + "','" + m + "','" + effective_date + "','" + company_id + "')";

cmd = ReplaceSpecialCharacters(cmd);

MySqlCommand sqlCmd = new MySqlCommand(cmd, sqlCon);

var i = sqlCmd.ExecuteNonQuery();

cmd = "";

}

}

else

{

if (item.Contains(";"))

{

List<string> parts = new List<string>(s.Split(';'));

cmd = "insert into quickcom values('" + destination + "','" + codes + "','" + prefix + "','" + rate + "','" + level + "','" + s + "','" + effective_date + "','" + company_id + "')";

cmd = ReplaceSpecialCharacters(cmd);

MySqlCommand sqlCmd = new MySqlCommand(cmd, sqlCon);

var i = sqlCmd.ExecuteNonQuery();

cmd = "";

}

}

}

help me...

thnx

网友答案:

if you have two delimiters to break each record and each item of the record, then you can directly get list of string list as below

string s = "a2-b2-c2-d2-e2;a3-b4-c2-d2-e2";
var list  = s.Split(';').Select(s1 => s1.Split('-').ToList()).ToList();
网友答案:

Not sure if this is what you need, but you can create a list whose items are in turns lists of strings by using the following type: List<List<string>>

网友答案:

Initialize a List, containing other Lists:

List<List<string>> g = new List<List<string>>();

And then just add your "parts" to this list:

g.Add(parts);

Hope I understood you correctly!

网友答案:

Your code above is only part, and doesn't work in my computer.
So I just write some comment:
1. List parts = new List(); parts is a varialble never used here.
2. I want to see the details of ReplaceSpecialCharacters.
3. cmd = "insert into ... you'd better replace it with string.Format("insert into ..{0}..{1}",para0,para1...)
4. List l = new List(s.Split(';')); it helps a lot if you list some examples of string s here.

网友答案:

Not sure what you want is List<List<string>> but that's what I'm assuming:

var l = new List<string>(s.Split(';'));
var aggregatedList = new List<List<string>>();

foreach (var item in l)
{
    if (item.IndexOf("-") > -1)
    {
        var a = item.Split('-').Select(n => int.Parse(n)).ToArray();
        var parts = Enumerable.Range(a[0], a[1]-a[0]+1).Select(i => i.ToString()).ToList();
        aggregatedList.Add(parts);
    }
}
相关阅读:
Top