none
How to take last 10 columns data from data table RRS feed

  • Question

  • programmatically how to take last 10 columns data from data table. please provide a sample code.
    Wednesday, October 9, 2019 3:51 PM

Answers

  • You have not said do you want the DataColumn instances or values of last 10 columns, but here is example of both. If you have strongly typed DataTable, DataRow and DataColumn, then you should use more specific type arguments.

    // Get last 10 data columns.
    DataColumn[] columns = table.Columns.OfType<DataColumn>().Skip(table.Columns.Count - 10).ToArray();
    
    // Get anonymous object where is row and values of last 10 columns per row.
    var rowLastColumns = table.Rows.OfType<DataRow>()
                                .Select(row => new
                                {
                                    Row = row,
                                    LastColumnValues = row.ItemArray.Skip(table.Columns.Count - 10).ToArray()
                                });
    
    // Or make new table with new rows and last 10 columns from original table
    DataTable destinationTable = new DataTable();
    
    foreach (DataColumn column in table.Columns.OfType<DataColumn>().Skip(table.Columns.Count - 10))
        destinationTable.Columns.Add(column.ColumnName, column.DataType);
    
    foreach (DataRow row in table.Rows)
        destinationTable.Rows.Add(row.ItemArray.Skip(table.Columns.Count - 10).ToArray());

    • Marked as answer by Sudip_inn Thursday, October 10, 2019 9:03 AM
    Thursday, October 10, 2019 5:53 AM
  • Hi Sudip_inn,

    Thank you for posting here. For your question, you want take last 10 columns data from data table. You could try the following code to get it.

        string constring = @"connString";
        SqlConnection con = new SqlConnection(constring);
        con.Open();
        string sql = "select * from T1";
    
        SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
        DataSet set = new DataSet();
        dataAdapter.Fill(set);
    
        DataTable dataTable = set.Tables[0];
        int columnCount = dataTable.Columns.Count;
        // Remove data that is not needed before
    
        for (int i = columnCount - 11; i >= 0; i--)
        {
            dataTable.Columns.Remove(dataTable.Columns[i]);
        }

    Or you can use a "List<string>" list to store each column.

        List<List<string>> list = new List<List<string>>();
    
        for (int i = dataTable.Columns.Count - 1; i > dataTable.Columns.Count - 11; i--)
        {
            DataColumn dc = dataTable.Columns[i];
            List<string> last10Col = (from d in dataTable.AsEnumerable() select d.Field<string>(dc.ColumnName)).ToList();
            list.Add(last10Col);
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 10, 2019 7:07 AM

All replies

  • Access ItemArray and Skip. In this case I take the last two columns.

    DataTable dt = ds.Tables["Customer"];
    var columns = dt.Rows[0].ItemArray.Skip(6);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 9, 2019 4:51 PM
    Moderator
  • Hello,

    Checking to see how you are doing with this issue.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 9, 2019 9:01 PM
    Moderator
  • You have not said do you want the DataColumn instances or values of last 10 columns, but here is example of both. If you have strongly typed DataTable, DataRow and DataColumn, then you should use more specific type arguments.

    // Get last 10 data columns.
    DataColumn[] columns = table.Columns.OfType<DataColumn>().Skip(table.Columns.Count - 10).ToArray();
    
    // Get anonymous object where is row and values of last 10 columns per row.
    var rowLastColumns = table.Rows.OfType<DataRow>()
                                .Select(row => new
                                {
                                    Row = row,
                                    LastColumnValues = row.ItemArray.Skip(table.Columns.Count - 10).ToArray()
                                });
    
    // Or make new table with new rows and last 10 columns from original table
    DataTable destinationTable = new DataTable();
    
    foreach (DataColumn column in table.Columns.OfType<DataColumn>().Skip(table.Columns.Count - 10))
        destinationTable.Columns.Add(column.ColumnName, column.DataType);
    
    foreach (DataRow row in table.Rows)
        destinationTable.Rows.Add(row.ItemArray.Skip(table.Columns.Count - 10).ToArray());

    • Marked as answer by Sudip_inn Thursday, October 10, 2019 9:03 AM
    Thursday, October 10, 2019 5:53 AM
  • Hi Sudip_inn,

    Thank you for posting here. For your question, you want take last 10 columns data from data table. You could try the following code to get it.

        string constring = @"connString";
        SqlConnection con = new SqlConnection(constring);
        con.Open();
        string sql = "select * from T1";
    
        SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
        DataSet set = new DataSet();
        dataAdapter.Fill(set);
    
        DataTable dataTable = set.Tables[0];
        int columnCount = dataTable.Columns.Count;
        // Remove data that is not needed before
    
        for (int i = columnCount - 11; i >= 0; i--)
        {
            dataTable.Columns.Remove(dataTable.Columns[i]);
        }

    Or you can use a "List<string>" list to store each column.

        List<List<string>> list = new List<List<string>>();
    
        for (int i = dataTable.Columns.Count - 1; i > dataTable.Columns.Count - 11; i--)
        {
            DataColumn dc = dataTable.Columns[i];
            List<string> last10Col = (from d in dataTable.AsEnumerable() select d.Field<string>(dc.ColumnName)).ToList();
            list.Add(last10Col);
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 10, 2019 7:07 AM
  • please provide full code.
    Thursday, October 10, 2019 9:04 AM