Click here to Skip to main content
15,924,317 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following items in my list and i want to sort this list in multiple sub lists for further process.

my list items are,

Table=Corporate_Name$$
Table=Customer_Name$$sanjay
Table=Cutomer_Name_For_Print_At_Bottom$$sanjay
Table1=Company_Name$$RWL Healthworld Limited
Table1=Bill_Status$$G
Table1=Home_Delievery$$N
Table2=User_Name$$Nazam
Table2=User_Name$$Nazam
Table2=User_Name$$Nazam
Table3=payment_value$$305.49
Table3=cash_amount_taken$$1000.00
Table3=cash_amount_returned$$694.51
Table3=TenderName$$Cash
Table3=TenderType$$1

I want to make sub lists like,
list 1 having items whose starts with Table,
list 2 having items whose starts with Table1,
list 3 having items whoes starts with Table2,
and so on; however the number of tables are

What I have tried:

Adding code

cmd = new MySqlCommand();
                                       cmd.CommandType = CommandType.StoredProcedure;
                                       cmd.CommandText = "sp_GetPosBillDetail_ForPrint";
                                       cmd.Parameters.Add(new MySqlParameter("inParam", onestr));
                                       cmd.Parameters.Add(new MySqlParameter("outParam", "@outParam"));
                                       cmd.Parameters["@outParam"].Direction = ParameterDirection.Output;
                                       cmd.Connection = con1;
                                       table = new DataTable();
                                       DataSet ds = new DataSet();
                                       adapter = new MySqlDataAdapter(cmd);
                                       adapter.Fill(ds);
                                       List<string> dtcol = new List<string>();

                                       for (int i = 0; i < ds.Tables.Count; i++)
                                       {
                                           foreach (DataColumn column in ds.Tables[i].Columns)
                                           {
                                               for (int m = 0; m < ds.Tables[i].Rows.Count; m++)
                                               {
                                                   dtcol.Add(ds.Tables[i] + "$$" + column + "$$" + ds.Tables[i].Rows[m][column].ToString());
                                               }
                                           }
                                       }


I have tried with writing switch case but not working properly.
C#
switch (dtcol[n].Substring(0,5))
                                           {
                                               case "Table":

                                                   break;

                                               case "Table1":                                                 
                                                      break;

                                               case "Table2":
                                                   break;  

but i dont know number of tables thats why this logic is not possible.
Posted
Updated 18-Jul-17 23:39pm
v3
Comments
RickZeeland 19-Jul-17 2:36am    
Are you using data from a DataTable ?
Member 11543226 19-Jul-17 2:39am    
yes
Member 11543226 19-Jul-17 2:41am    
adding previous code pls have look

try
List<string> lst = new List<string>();
          lst.Add("Table=Corporate_Name$$");
          lst.Add("Table=Customer_Name$$sanjay");
          lst.Add("Table=Cutomer_Name_For_Print_At_Bottom$$sanjay");
          lst.Add("Table1=Company_Name$$RWL Healthworld Limited");
          lst.Add("Table1=Bill_Status$$G");
          lst.Add("Table1=Home_Delievery$$N");
          lst.Add("Table2=User_Name$$Nazam");
          lst.Add("Table2=User_Name$$Nazam");
          lst.Add("Table2=User_Name$$Nazam");
          lst.Add("Table3=payment_value$$305.49");
          lst.Add("Table3=cash_amount_taken$$1000.00");
          lst.Add("Table3=cash_amount_returned$$694.51");
          lst.Add("Table3=TenderName$$Cash");
          lst.Add("Table3=TenderType$$1)");

          string[] keys = lst.Select(k => k.Split('=')[0]).Distinct().ToArray();
          Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
          foreach (string key in keys)
          {
              dict.Add(key, lst.Where(k => k.StartsWith(key + "=")).ToList());
          }
 
Share this answer
 
Comments
Member 11543226 19-Jul-17 3:22am    
NOT WORKING
Karthik_Mahalingam 19-Jul-17 3:31am    
what is the value you are getting in dtcol ?
Member 11543226 19-Jul-17 5:53am    
[Table$$Corporate_Name$$, System.Collections.Generic.List`1[System.String]]

same data with addition "System.Collections.Generic.List`1[System.String]]"
Karthik_Mahalingam 19-Jul-17 6:42am    
i mean the string values
I think you need something like:
C#
switch (dtcol[n].Substring(0,7)) 
{ 
  case "$$Table$":
...
  case "$$Table1":
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900