C # DataTable duplicate data efficiency filter method

C # DataTable duplicate data efficiency filter method

Use the DataView, then set Totable, several fields and setting a Boolean value, which represents the field as a whole can not be duplicated in this table , the sample code:

Copy the code

namespace A
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id",typeof(int));
            Random r = new Random(DateTime.Now.Millisecond);
            // 1000 rows randomly generated
            for (int i = 0; i < 1001; i++)
            {
                dt.Rows.Add(r.Next(1, 11));
            }
 
            DataView dv = new DataView(dt);
            dt = dv.ToTable (true, "Id"); // for filter Id, true indication distinct methods
 
            foreach (DataRow item in dt.Rows)
            {
                Console.WriteLine(item["Id"].ToString());
            }
        }
    }
}

Copy the code

 The above datatable only one, if it is multi-column, then go heavy follows

DataView dv = new DataView(dt);
dt = dv.ToTable(true, new string[] { "Id","Value" });

 

Guess you like

Origin blog.csdn.net/cxu123321/article/details/93764796