Tuesday, May 7, 2013

Extract a unique list of items from System.Data.DataTable

 
results is a datatable containing some rows. I want a list of strings of distinct values extracted from the column MyColumn:
List<string> columns = (from row in results.AsEnumerable()
                       select row.Field<string>("MyColumn")).Distinct().ToList<string>();

If the list<string> already contains duplicate values, use a hashset to get them out:

List<string> allColumns = new List<string>();

//add a buncha items to this list. //Then get out the unique values:


var unique_items = new HashSet<string>(allColumns);

No comments:

Post a Comment