I am trying to get distinct FullNames from a list that contains FullNames and IDs then displaying these in a listBox control. Is there a simple way to do it? Thanks Ben
using (DB2DataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
Contact contact = new Contact();
contact.ContactID = Convert.ToInt32(dr["CONTACT_ID"]);
contact.FullName= dr["FULL_NAME"].ToString();
myContacts.Add(contact);
//contactsListBox.ItemsSource = myContacts.Distinct FullName??
}
}
With LINQ:
should work. If the order is unimportant you could also use:
(and then use
ToList()
/OrderBy
whatever you need)I think you can use different approaches here:
Make Sql query that queries distinct values.
Check that contact already in list. This approach assume that your class must redefine euqality operator. Or you can check that this contact id is already in list.
Use Linq query as mentioned above.