locked
distinct column RRS feed

  • Question

  • User259980215 posted

    I have a returned dataset and I want to add the column fullname to a dropdown, the problem is that the column has duplicates how can I get the distinct values from this column in the dataset?  Below is the code is therr a way to get the distinct fullname? NOTE: the below code works fine, I just need the distinct fullname

     

    Dim m_dsContact As DataSet = m_oSiteInfo.GetPPSOrgStaff(m_intProgProvSiteID)

     Dim adc1 As DataColumn adc1 = New DataColumn("fullname", System.Type.GetType("System.String"))

    m_dsContact.Tables(0).Columns.Add(adc1)

               Dim intRowNumber As Integer

                intRowNumber = 0

           For Each Row As DataRow In m_dsContact.Tables(0).Rows

                    m_dsContact.Tables(0).Rows(intRowNumber).Item(fullname") = m_dsContact.Tables(0).Rows(intRowNumber).Item("first_name") & _

                    " " & m_dsContact.Tables(0).Rows(intRowNumber).Item("last_name")

                    intRowNumber = intRowNumber + 1

               Next

               

    'Unique values for the list.

      Dim strColNames() As String = {"fullname", "Contact_ID"}

      Me.ddlAdministeredTestStaff.DataSource = m_dsContact.Tables(0).DefaultView.ToTable(True, strColNames)

           Me.ddlAdministeredTestStaff.DataTextField = "fullname")

            Me.ddlAdministeredTestStaff.DataValueField = "Contact_ID"

             Me.ddlAdministeredTestStaff.DataBind()

             Me.ddlAdministeredTestStaff.Items.Insert(0, "Choose a Contact")

               Me.ddlAdministeredTestStaff.Items(0).Value = 0

               Me.ddlAdministeredTestStaff.SelectedIndex = 0

    Monday, August 24, 2015 4:27 PM

Answers

  • User-1716253493 posted

    Change this

    For Each Row As DataRow In m_dsContact.Tables(0).AsDataView.ToTable(True, "fullname").Rows

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2015 9:24 PM

All replies

  • User475983607 posted

    Give this a try...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    
    namespace TestConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = CreateDataTable();
    
                //Get a list of full names
                List<Contact> Contacts = dt.AsEnumerable().Select(x => new Contact
                {
                    FullName = String.Format("{0} {1}", (string)x["First_Name"], (string)x["Last_Name"]),
                    ContactId = int.Parse(x["Contact_ID"].ToString())
                }).ToList();
    
                foreach (Contact dc in Contacts)
                {
                    Console.WriteLine("{0} {1}", dc.ContactId, dc.FullName);
                }
    
                Console.WriteLine();
    
                //Get a distinct list
                IEnumerable<Contact> distinctContact = Contacts.Distinct();
                foreach (Contact dc in distinctContact)
                {
                    Console.WriteLine("{0} {1}", dc.ContactId, dc.FullName);
                }
    
            }
    
    
    
            static DataTable CreateDataTable()        
            {
                DataTable dt = new DataTable();
                dt.Clear();
                dt.Columns.Add("First_Name");
                dt.Columns.Add("Last_Name");
                dt.Columns.Add("Contact_ID");
                DataRow dr = dt.NewRow();
                dr["First_Name"] = "John";
                dr["LAst_Name"] = "Smith";
                dr["Contact_ID"] = 1;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["First_Name"] = "John";
                dr["LAst_Name"] = "Smith";
                dr["Contact_ID"] = 1;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["First_Name"] = "John";
                dr["LAst_Name"] = "Smith";
                dr["Contact_ID"] = 1;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["First_Name"] = "John";
                dr["LAst_Name"] = "Doe";
                dr["Contact_ID"] = 2;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["First_Name"] = "John";
                dr["LAst_Name"] = "Doe";
                dr["Contact_ID"] = 2;
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["First_Name"] = "Jane";
                dr["LAst_Name"] = "Doe";
                dr["Contact_ID"] = 3;
                dt.Rows.Add(dr);
    
                return dt;
            }
        }
    
        public class Contact : IEquatable<Contact>
        {
            public string FullName { get; set; }
            public int ContactId { get; set; }
    
            public bool Equals(Contact other)
            {
    
                if (Object.ReferenceEquals(other, null)) return false;
    
                if (Object.ReferenceEquals(this, other)) return true;
    
                return ContactId.Equals(other.ContactId) && FullName.Equals(other.FullName);
            }
    
    
            public override int GetHashCode()
            {
    
                int hashProductName = FullName == null ? 0 : FullName.GetHashCode();
    
                int hashProductCode = ContactId.GetHashCode();
    
                return hashProductName ^ hashProductCode;
            }
        }
    }
    

    Monday, August 24, 2015 5:57 PM
  • User259980215 posted

    I am using VB not C#

    Monday, August 24, 2015 7:27 PM
  • User-1716253493 posted

    Change this

    For Each Row As DataRow In m_dsContact.Tables(0).AsDataView.ToTable(True, "fullname").Rows

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 24, 2015 9:24 PM