Home > database >  Extract elements from List string built using c #
Extract elements from List string built using c #

Time:10-21

I have this string

Dispatching System,proposal,to be sent,main,2022-006,related,2022-017,related

that is composed of this c# code

List<string> value1 = new List<string>();
foreach (string item in Request.Form)
{
    if (item.Contains("ddl"))
    {
        value1.Add(Request.Form[item]);
    }
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('value1:\\n"  
    string.Join(",", value1)   "');", true);

Using the code above the output is

Dispatching System,proposal,to be sent,main,2022-006,related,2022-017,related

Now I need to extract from this string from element number 5 to all subsequent elements, that is

2022-006,related,2022-007,related

and storing a row for each string value in a database table, that is

t q
2022-006 related
2022-017 related

Expected output

2022-006 
related 
2022-017 
related

But the expected ouput now is empty...

This is my c# code

List<string> value1 = new List<string>();
foreach (string item in Request.Form)
{
    if (item.Contains("ddl"))
    {
        value1.Add(Request.Form[item]);
        List<string> value2 = item.Split(',').ToList();

        for (int i = 4; i < value2.Count; i  )
        {
            //Insert into db
            Response.Write(value2[i]   "<br />"   value2[i   1]   "<br /><br />");
            i  ;
        }
    }
}

Thanks in advance for any help, really appreciated.

Solution

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

foreach (string item in Request.Form)
{
    if (item.Contains("ddl"))
    {
        value1.Add(Request.Form[item]);
    }
}

var requestDLL = string.Join(",", value1);
var value2 = requestDLL.Split(',');

for (int i = 4; i < value2.Length; i  )
{
    //Insert into db
    Response.Write(value2[i]   "<br />"   value2[i   1]   "<br /><br />");
    i  ;
}

Output

2022-006 
related 
2022-017 
related

CodePudding user response:

Here you can find the code that return the expected output https://dotnetfiddle.net/8aBrZj

  • Related