none
Select Distinct Rows from Data Table RRS feed

  • Question

  • I have created a datatable by name table1,

    DataTable table1 = new DataTable("Student");
                table1.Columns.Add("Id");
                table1.Columns.Add("Name");
                table1.Columns.Add("Age");

                table1.Rows.Add(1, "ABC", 33);
                table1.Rows.Add(2, "PQR", 24);
                table1.Rows.Add(3, "ABC", 31);
                table1.Rows.Add(4, "XYZ", 23);
                table1.Rows.Add(5, "PQR", 23);
                table1.Rows.Add(6, "ABC", 33);

    Here I need the Output as;

    Name                   Age

    ABC                      33

    ABC                      31

    PQR                      24

    PQR                      23

    How can i sort out the data here,Can any one Let me know..

    DataSet set = new DataSet(); set.Tables.Add(table1); if (set != null) { DataView view = new DataView(table1); DataTable dt = view.ToTable(true, "Name", "Age"); for (int i = dt.Rows.Count - 1; i >= 0; i--) { DataRow dr = dt.Rows[i]; if (dr["Name"] == "XYZ") dt.Rows.Remove(dr); }

    but how can we do this through DataTable?



    sayeed 


    Monday, June 10, 2013 11:41 AM

Answers

All replies

  • You can use the DataTable.Select method to get an array of DataRows matching a specified filter:

          DataTable table1 = new DataTable("Student");
          table1.Columns.Add("Id");
          table1.Columns.Add("Name");
          table1.Columns.Add("Age");
          table1.Rows.Add(1, "ABC", 33);
          table1.Rows.Add(2, "PQR", 24);
          table1.Rows.Add(3, "ABC", 31);
          table1.Rows.Add(4, "XYZ", 23);
          table1.Rows.Add(5, "PQR", 23);
          table1.Rows.Add(6, "ABC", 33);
          DataRow[] dataRows = table1.Select("Name = 'ABC' OR Name = 'PQR'");

    Monday, June 10, 2013 1:38 PM
  • How can I select the conditions in this?It should satisfy the conditions how to compare the single DataTable which is if PQR is having age as 24 and 23 both the conditions should match and same for ABC and if there is alone value that is XYZ then we have to ignore that?

    sayeed 


    Tuesday, June 11, 2013 4:44 AM
  • The DefaultView (that is a DataView object).ToTable command contains some overloads to return a distinct table.

    http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx

     

    Success
    Cor

    Tuesday, June 11, 2013 6:33 AM
  • Alternately, you can get a distinct list of Name and Age (which I think is what you want), and then later add the Id field again, and reset them like so:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable table1 = new DataTable("Student");
                table1.Columns.Add("Id");
                table1.Columns.Add("Name");
                table1.Columns.Add("Age");
    
                table1.Rows.Add(1, "ABC", 33);
                table1.Rows.Add(2, "PQR", 24);
                table1.Rows.Add(3, "ABC", 31);
                table1.Rows.Add(4, "XYZ", 23);
                table1.Rows.Add(5, "PQR", 23);
                table1.Rows.Add(6, "ABC", 33);
    
                DataTable newTable = new DataView(table1, "", "name asc", DataViewRowState.CurrentRows)
                    .ToTable(true, "Name", "Age");
    
                //Add ID
                newTable.Columns.Add("Id", typeof(Int32));
                for (Int32 i = 0; i < newTable.Rows.Count; i++)
                    newTable.Rows[i].SetField("Id", i + 1);
                
    
    
                
            }
        }
    }
    

    Tuesday, June 11, 2013 3:02 PM