简体   繁体   中英

how to create list each item as another list?

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();

不知道这是否是您所需要的,但是可以使用以下类型创建一个列表,该列表的项目依次为字符串List<List<string>>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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM