Get distinct values from a column of DataTable in .NET 2.0

a 夏天 提交于 2019-12-01 21:20:19
// Given a data table:
var dt = new DataTable();
dt.Columns.Add("ITEMNO");
dt.Rows.Add("1 ");
dt.Rows.Add(" 1");
dt.Rows.Add("2");

var dict = new Dictionary<string, bool>();

foreach(DataRow dr in dt.Rows)
{
    var itemNo = dr["ITEMNO"].ToString().Trim();

    // Take advantage of O(1) lookup:
    if (!dict.ContainsKey(itemNo))
    {
        dict.Add(itemNo, true);
    }
}

// Get list from dictionary keys:
var items = new List<string>(dict.Keys);

If you can install .Net 3.5 on the server, and reference System.Core.dll in your application, you can leverage HashSets which would modify the above code to:

var hashSet = new HashSet<string>();

foreach(DataRow dr in dt.Rows)
{
    var itemNo = dr["ITEMNO"].ToString().Trim();    

    // Only unique elements are added to the hash set, 
    // no need to check for duplicates
    hashSet.Add(itemNo);    
}

var items = new List<string>(hashSet);

The benefit of using HashSet over a Dictionary is admittedly trivial, but I'd prefer it since I don't care for the arbitrary bool value in the dictionary, but you'd need to meet the .Net 3.5 and reference requisites.

To get distinct values form a column you can use this method:

List<T> SelectDistict<T>(DataTable table, string column)
{
    DataTable temp = new DataView(table).ToTable(true, column);
    List<T> items = new List<T>();
    foreach (DataRow row in temp.Rows)
        items.Add(row.Field<T>(column));
    return items;
}

In above method I used DataView.ToTable which by passing true as first argument, selects distinct values.

Here is the usage example:

List<string> items = SelectDistict<string>(yourDataTable, "ITEMNO");

Note

If you need to trim values, you can change above code and first create a clone copy of the DataTable. Then add a computed column which contains, trimmed value from the given column name for distinct values by assigning TRIM(column) to Expression property of column. Then follow the steps using the new trimmed column like above code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!