Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a list of
C#
List<Person> people = new List<>


Object Person looks as follows:
C#
public string Name{ get; set; }
public string Surname{ get; set; }


What I have tried:

How could I compare the two values of Name and Surname?
for example, List at index 0 have:
Name = "blah"
Surname = "nah"

For example: if name 'blah' matches the same index as surname 'nah' do something.

EDIT:

Lets say I have two textboxes, one textbox displays the Name and the user have to type in the surname. The surname needs to match the same index as the Name is in. So if Name is 'blah' and the user types in the Surname which is already 'Nah'(in the same index) then do something, for example display MessageBox. You can treat that as a guessing game, what is his surname/name.

I forgot to mention that List item is being randomly generated. Here is the code:

C#
randomPerson= people[random.Next(people.Count)];
string result = RandomlyGenerated(random);


This is the random method:

C#
private string RandomlyGenerated(Random r)
        {
            if (random.Next(1) == 0)
            {
                txtName.Text = randomPerson.Name;
                return randomPerson.Name;
            }
            else
            {
                txtSurname.Text = randomPerson.Surname;
                return randomPerson.Surname;
            }
        }


Maybe this way it will be more clear. Basically if txtName displays text. The user have to guess what is the corresponding surname that is in the same index as the name which was displayed on screen.

So if we have in Index 0:
Name = 'blah'
Surname = 'nah'

The user should enter 'nah' as the surname to, for example message box to appear. Hope it will make it more clearer now.

Still struggling with it :/.
Posted
Updated 31-Oct-17 10:21am
v5
Comments
BillWoodruff 30-Oct-17 21:57pm    
Please clarify if you want to compare two instances of 'Person to see if they have identical field values ... or, do you want to search the List of Person for some condition, or to find matches of some type based on field values.

Those are two different goals.
Karthik_Mahalingam 30-Oct-17 22:21pm    
What is your expected output?
Post some sample data..
Question is not clear..

Note: this code was written quickly, and, while it compiles, I have not tested it thoroughly.

First, if your code is creating the list of instances of 'Person and you want to enforce some screening rule about names, here's an example:
public class Person
{
    private static List<string> surnamesInUse = new List<string>();
    private static List<string> firstNamesInUse = new List<string>();

    private string lname;
    private string lsurname;

    public Person(string name, string surname)
    {
        if (name == String.Empty || surname == String.Empty)
        {
            throw new ArgumentException($"name and surname cannot be empty strings");
        }

        lname = name.ToLower();
        lsurname = surname.ToLower();

        if (lname == lsurname)
        {
            throw new ArgumentException($"name and surname cannot match independent of case");
        }

        if (firstNamesInUse.Contains(lname))
        {
           throw new DuplicateNameException($"{name} is already in use as a first name");
        }

        if (firstNamesInUse.Contains(lsurname))
        {
            throw new DuplicateNameException($"{surname} is already in use as a first name");
        }

        if (surnamesInUse.Contains(lname))
        {
            throw new DuplicateNameException($"{name} is already in use as a surname");
        }

        if (surnamesInUse.Contains(lsurname))
        {
            throw new DuplicateNameException($"{surname} is already in use as a surname");
        }

        surnamesInUse.Add(lsurname);
        firstNamesInUse.Add(lname);

        Name = name;
        Surname = surname;
    }

    public string Name { get; set; }
    public string Surname { get; set; }

    public bool Equals(Person p2)
    {
        return
            lname == p2.Name.ToLower()
            &&
            lsurname == p2.Surname.ToLower();
    }
}
If you did not create the instances of 'Person, and want to look for matches, here's some ideas:
public enum MatchType
{
    NoMatch,
    SurnameMatch,
    FirstNameMatch,
    BothNameMatch
}

public static class PersonMatcher
{
    public static MatchType ComparePersons(Person p1, Person p2)
    {
        if (p1.Surname == p2.Surname)
        {
            if (p1.Name == p2.Name) return MatchType.BothNameMatch;

            return MatchType.SurnameMatch;
        }

        if (p1.Name == p2.Name) return MatchType.FirstNameMatch;

        return MatchType.NoMatch;
    }

    public static bool IsUnique(Person p1, List<Person> persons)
    {
        return ! persons.Any(p2 => (! p2.Equals(p1)) && ComparePersons(p2, p1) != MatchType.NoMatch);
    }

    public static IEnumerable<Person> GetDistinct(List<Person> persons)
    {
        return persons.Where(p1 => IsUnique(p1, persons));
    }

    public static List<Person> GetNotMatching(Person p1, List<Person> ps)
    {
        return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.NoMatch).ToList();
    }

    public static List<Person> GetSurnameMatches(Person p1, List<Person> ps)
    {
        return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.SurnameMatch).ToList();
    }

    public static List<Person> GetFirstNameMatches(Person p1, List<Person> ps)
    {
        return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.FirstNameMatch).ToList();
    }

    public static List<Person> GetDuplicates(Person p1, List<Person> ps)
    {
        return ps.Where(p2 => (!p2.Equals(p1)) && ComparePersons(p1, p2) == MatchType.BothNameMatch).ToList();
    }
}
You could easily turn these methods into Extension Methods, and/or modify to compare without case sensitivity via conversion to lower-case.
 
Share this answer
 
v2
Comments
fellanmorgh 31-Oct-17 17:04pm    
Hi, not what I was looking for. Was able to get it work anyway...still thanks for that code. It gave me idea to add some useful code to the program! :)
BillWoodruff 31-Oct-17 20:54pm    
Glad you found the code useful in some way !
Something that looks like this:
foreach (Person person in people)
{
    if (person.Name == person.Surname)
    {
        // do something..
    }
}

also check out this How to Create a List of Objects in C#[^]
 
Share this answer
 
refer in line comments
int index = 2;  // for the index to be checked
if (lst.Count > index)  // validate the index
{
    Person obj = lst[index]; // index of the person in the list
    if (obj.Name == obj.Surname) // compare the Name and Surname
    {
        // match
    }
    {
        // not equal
    }
}
 
Share this answer
 
Comments
fellanmorgh 31-Oct-17 17:03pm    
Hi Your solution actually was the closest. Just needed to play around with it a little bit. Was just overthinking it. Thanks! :)
Karthik_Mahalingam 31-Oct-17 21:48pm    
Welcome

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