Click here to Skip to main content
16,006,013 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm trying to understand how to check duplicates names in a list(of T). I have a name that's not in the list yet, and before I put it in the list, I need to check for duplicates before adding the new information to the list.

Here's some code that Griff helped me out with yesterday.

Dim contacts As New List(Of Contact)()
Dim newContact As New Contact()

newContact.FirstName = "joe"
newContact.LastName = "smith"
newContact.PhoneNumber = "234345435"
newContact.Birthday = Date.Now.AddYears(-20)
contacts.Add(newContact)

What I want to know is, does the last name in the existing class match the the last name that's in memory that I haven't instantiated yet?

I appreciate any help at all,
thanks

What I have tried:

I've tried the above code and I've been searching the net for days. Most of the answers are about using a predicate and I don't know how to use that either. So I would like to know how to do it without it first if possible.
Posted
Updated 13-Apr-16 2:22am
Comments
F-ES Sitecore 13-Apr-16 7:56am    
I won't post this as a solution as I don't know the vb.net, but it's going to be like

if not contacts.Any(c => c.LastName.Equals(nameToCheck)) then
' lastname doesn't exist in the collection
end if

If you check out the overloads of Equals you can make it case insensitive if that's what you need.
Sergey Alexandrovich Kryukov 13-Apr-16 9:07am    
Why not avoiding duplicates in first place, when you add the items?
If you need fast search in pretty big data sets, lists are not the best. Then you should consider associative collections based on hash; dictionaries, sets, sorted lists, sorted dictionaries...
—SA

1 solution

Try the following:
VB
If Not contacts.Any(Function(obj) obj.LastName = "smith") Then
    Dim newContact As New Contact()
    newContact.FirstName = "joe"
    newContact.LastName = "smith"
    newContact.PhoneNumber = "234345435"
    newContact.Birthday = Date.Now.AddYears(-20)
    contacts.Add(newContact)
End If

The bit in brackets after Any is a Lambda expression[^]
It's essentially saying - here is a Function that is going to take a parameter obj and with obj we want to look at the LastName property and compare it to the text "smith"(you would probably want to convert to lowercase or capitals before doing the comparison).

The compiler knows that LastName can be a property of obj because we are using contacts.Any and it knows that contacts is a List of Contact. We are using .Any because we don't actually need a list of matches, we just need to know if there is a match.
And note the Not because the .Any will return True if it does find a match
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Apr-16 9:10am    
5ed.
Two points: 1) if the task of removing duplicates appears, it usually indicates a big flaw in a wider-scope design; duplicates should be prevented, not detected and removed; 2) for bigger data sets and search, lists are not suitable; the associative collections based on hash and buckets should be used.
—SA
CHill60 13-Apr-16 9:11am    
Thank you - and well said on the other points raised.
jumper777 13-Apr-16 12:04pm    
Thank you Sergey. I am trying to avoid duplicates. And yes it's before the record is saved to the list. I try to validate when the save button is clicked. This is when I'm trying to decide whether to save the data or not.

Thanks for telling me about the "Any" part. I'm pretty new at lists. And I've heard about Lambda, but haven't studied it yet. At them moment, this program is not a real world program. I'm trying to teach myself concepts right now so when an actual program comes up, I will be prepared. I'm going to try your solution right now.
Thank you very much.
jumper777 13-Apr-16 12:13pm    
Sergey, you are my hero man. I just marked your answer as the accepted solution. I did have to change one "tiny" part. The "NOT" operator that you talked about is actually reversed. If you use Not, it will add the the new data to the list, but if you remove it, it does not. But it only took me a one time through the debugger to find that out.

Can't thank you enough.
CHill60 13-Apr-16 16:17pm    
That's good use of the debugger. Nice one.
However I'm not SA ;-)

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