Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a list that is populated from database:

C#
public List<string> listUserFirstName = new List<string>();

 while (reader.Read())
 {
     listUserFirstName.Add(reader["FirstName"].ToString().ToUpper());

     if (listUserFirstName.Capacity >= 2 )
     {
         msg = "Duplicate Names!";
     }
 }


I want to check for duplicate names, i.e. anytime its content is more than one, the msg should show, the above code doesnt give me the desired result. Any assistance would be appreciated.
Posted
Updated 16-Apr-14 11:37am
v2

public List<string> names = new List<string>();

while(reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    if (!names.Contains(name))
        names.Add(names);
    else
    {
        // handle reporting the duplicate here
    }
}


If the list is large, then consider using a Dictionary or a HashSet instead of a List to improve performance.

If all you need is a list of the names with no duplicates then
public List<string> names = new List<string>();

while(reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    names.Add(names);
    names = names.Distinct().ToList();
}
 
Share this answer
 
v3
Comments
James_Finch 16-Apr-14 15:39pm    
Nice Matthew. This is what comes of knowing the methods of the FCL classes well. My method was doing the .Contains() manually (and probably not as efficiently).
Uwakpeter 17-Apr-14 6:07am    
Thanks for your reply, i want to display duplicate names for user to choose from, then if it is not duplicate, then i update the name.
This or something similar should suit:

C#
public List<string> names = new List<string>();

while (reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    if (names.Count == 0)
    {
        names.Add(name);
    }
    else
    {
        bool duplicate;

        foreach (string n in names)
        {
            if (n == name)
                duplicate = true;
        }

        if (!duplicate)
            names.Add(name);
    }
}


Here you are always adding the string if the list is empty, then each time you read a name you are only adding it if it does not exist in the list. This way you will never produce a list with duplicates in it.

If this is not what you want then I'm afraid I have misunderstood your request.

Good luck. :)
 
Share this answer
 
v2
Comments
Uwakpeter 17-Apr-14 6:07am    
Thanks for your reply, i want to display duplicate names for user to choose from, then if it is not duplicate, then i update the name.
James_Finch 17-Apr-14 6:56am    
I presume there is some other information in the record, not just a name? Something has to be unique or it won't matter which one they pick. If you have only names then it is best to simply avoid duplication.

If you do have more than the name, for example a phone number as well, I would create a small type (a struct would do) to represent your records and compare those. I've posted this as a seperate solution.
Based on your comment, here is an updated solution:

Create a type in your namespace, similar to this:

C#
struct Record
{
    public string Name;
    public string PhoneNumber;
}


Then in your method, compare the type values:

C#
public List<Record> Records = new List<Record>();

while (reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    string phoneNumber = reader["PhoneNumber"].ToString();
    Record r = new Record();

    if (names.Count == 0)
    {
        r.Name = name;
        r.PhoneNumber = phoneNumber;
        Records.Add(r);
    }
    else
    {
        bool duplicate;

        foreach (Record record in Records)
        {
            if (record.Name == name &&
                record.PhoneNumber == phoneNumber)
                duplicate = true;
        }

        if (!duplicate)
            Records.Add(r);
        else
        {
            // Use whatever means you see fit here to
            // ask the user if they wish to replce the
            // existing record
        }
    }
}
 
Share this answer
 
v2
Comments
Uwakpeter 17-Apr-14 7:18am    
Thanks James, the application is to upload students' passport to sql server database, there is no unique information given accept that the passports are named in this order: Surname_Firstname.jpg, so i am spliting the two names, and now used it to compare with the ones in the database, if there is a match, the system will update the passport field with image passport, but there are intances where two or more students have the same Surname and Firstname, in this case, i want to display all the matching names so the user could choose/select the one that the passport should be updated against. Thanks
James_Finch 17-Apr-14 7:41am    
Well I'm afraid that makes no sense. If you change PhoneNumber to LastName in my solution it will work for you. You just need to code some method of allowing the user to choose a record in the else clause. But if all you can display to the user is two identical names, how are they to know exactly who's record they might be overwriting? For this to work, there needs to be something unique. Since you refer to 'passport' I'm presuming that's exactly what you say it is. There should be all sorts of unique information like date of birth, address, passport ID, etc.

In this case I would modify Record to include all these fields and create a nested loop declaring a new list for each record in your database. Each time, it can iterate through the list and store every matching first and last name. Then I would add each to a ListBox and allow the user to select the 'correct' record (if any) to update.

For example:

Finch, James : age 29 : DOB 29/06/1984
Finch, James : age 20 : DOB 29/06/1993

But I repeat, there needs to be something unique or this is just not a good idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900