简体   繁体   English

如何将每个项目创建为另一个列表?

[英]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(); 1.列表部分= new List(); parts is a varialble never used here. 零件是这里从未使用过的变量。
2. I want to see the details of ReplaceSpecialCharacters . 2.我想查看ReplaceSpecialCharacters的详细信息。
3. cmd = "insert into ... you'd better replace it with string.Format ("insert into ..{0}..{1}",para0,para1...) 3. cmd =“插入...,最好用string.Format替换(”插入.. {0} .. {1}“,para0,para1 ...)
4. List l = new List( s .Split(';')); 4.列表L =新列表(一个或多个 .Split( ';')); it helps a lot if you list some examples of string s here. 如果您在此处列出一些string 示例,它将很有帮助。

Not sure what you want is List<List<string>> but that's what I'm assuming: 不确定您想要的是List<List<string>>但这就是我所假设的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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