Q:

Removing duplicates from an array populated by a reader

I have a database column that has several names separated by commas in each row. I'm trying to populate a data-table containing all the names but without duplicates.
Currently the list populates but duplicate names still appear.
My code is:
while (reader.Read())
        {
            string[] array = reader[0].ToString().Split(',');
            string[] unique = array.Distinct().ToArray();
            foreach (string s in unique)
                dt.Rows.Add(s);
        }

Any help would be greatly appreciated, thanks.

A:

Try this
List<string> distinctValues = new List<string>();

while (reader.Read())
{
    string[] unique = reader[0].ToString().Split(',').Distinct().ToArray();

    foreach (string s in unique)
        if (!distinctValues.Contains(s))
            distinctValues.Add(s);
}

foreach (string s in distinctValues)
    dt.Rows.Add(s)

