How do I display unique items in Repeater?

I'm building a repeater, code listed below.  How do I only show unique items in the list?
JT_SIROAsked:
Who is Participating?
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, also can be done in this way (based on your example):
List<string> uniqueCategories = new List<string>();
foreach (DataRow row in dt.Rows)
{
    intCountBadCategories = intCountBadCategories + 1;

    string category = row["Category"].ToString();
    // Check to see if this is a valid category AND it dont exists in the list.
    if (!IsValidCategory(category) && !uniqueCategories.Contains(category))
        uniqueCategories.Add(category);
}
// Add to Repeater
foreach (string value in uniqueCategories)
    dt3.Rows.Add(value);

Open in new window

0
 
MlandaTCommented:
No code posted ;)
0
 
JT_SIROAuthor Commented:
oops
        foreach (DataRow row in dt.Rows)
        {
            intCountBadCategories = intCountBadCategories + 1;

            // Check to see if this is a valid category
            if (IsValidCategory(row["Category"].ToString()) == false)
            {
                // Add to Repeater
                DataRow newRow3 = dt3.NewRow();
                newRow3["Category"] = row["Category"].ToString();
                dt3.Rows.Add(newRow3);
            }
        }

Open in new window

0
 
MlandaTCommented:
If dt is populated from the database, the best place would be to use a SELECT DISTINCT in the code loading data into the datatable, dt.
0
 
MlandaTCommented:
otherwise, check if the value already exists in dt3. Something like:
if (IsValidCategory(row["Category"].ToString()) == false && dt3.Select("Category = '" + row["Category"].ToString().Replace("'","''") + "'").Length = 0)

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.