locked
Fastest way to iterate a DataTable RRS feed

  • Question

  • D	D	D	T	T	T	T	T	T	T	T
    
    a	b	c	1	2	3	4	5	6	7	8
    
    a	a	a	3	4	5	6	7	8	9	0
    
    c	b	a	4	5	6	8	7	8	8	9
    
    
    
    

    I have the following situation and would like to know the fastest approach to get each single row from a DataTable.

    1. I have a DataTable filled with data.
    2. I do NOT know how many columns with D I have (can be 1 - many), so I have to find this out first.
    3. I do NOT know how many columns with T I have so dito.

    4. I want to create an ID based on concatenating all D e.g. D_D_D
    5. I want o load all values of the T columns into an array of doubles.

    Things to be solved:
    - find number of D columns
    - find number of T columns
    - iterate through all rows and create D_D_D_ + double[] per row - how to know that a NEW D_D_D started?
    - add to arraylist or such.

    I need this to be fast and also I cannot use any other approach. This is what I have to solve.

    Any help/code is highly appreciated.

    Thanks,
    Dirk

     

     

     

     

    Friday, September 10, 2010 8:37 AM

Answers

  • The whole problem will be solved if you just find the number columns "D" and "T"

    first move all the columns of your table into a generic list

    List<DataColumn> columns = new List<DataColumn>();

    foreach (DataColumn col in yourTable.Columns)

          columns.Add(col);

     

    //using LINQ expressions, find the number of columns containing D and T

     int? countD = columns.Where(c => c.ColumnName.IndexOf("D") > 0).Count();

     int? countT = columns.Where(c => c.ColumnName.IndexOf("T") > 0).Count();

     

    then you can create the new Id depending on the count of each column

     

    Regards,

    • Marked as answer by Bin-ze Zhao Monday, September 13, 2010 9:31 AM
    Friday, September 10, 2010 9:59 AM

All replies

  • The whole problem will be solved if you just find the number columns "D" and "T"

    first move all the columns of your table into a generic list

    List<DataColumn> columns = new List<DataColumn>();

    foreach (DataColumn col in yourTable.Columns)

          columns.Add(col);

     

    //using LINQ expressions, find the number of columns containing D and T

     int? countD = columns.Where(c => c.ColumnName.IndexOf("D") > 0).Count();

     int? countT = columns.Where(c => c.ColumnName.IndexOf("T") > 0).Count();

     

    then you can create the new Id depending on the count of each column

     

    Regards,

    • Marked as answer by Bin-ze Zhao Monday, September 13, 2010 9:31 AM
    Friday, September 10, 2010 9:59 AM
  • VS2008:
    ==============
    List<DataColumn> columns = new List<DataColumn>();

                foreach (DataColumn col in yourTable.Columns)
          columns.Add(col);


                int? countD = columns.Where(c => c.ColumnName.IndexOf("D") == 0).Count();

                int? countT = columns.Where(c => c.ColumnName.IndexOf("T") == 0).Count();


    • Proposed as answer by R.A.J.Y.A.M Tuesday, September 14, 2010 8:11 PM
    Friday, September 10, 2010 6:27 PM