Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List<>.Find() Question 2

Status
Not open for further replies.

MadJock

Programmer
May 25, 2001
318
GB
Hi,

I have a list of objects that I would like to search for dupluicates based on several criteria. Simplified example below:

Code:
List<Person> people = new List<Person>();
Person p1 = new Person("Joe", "Bloggs", 29, "1 Elm Street");
Person p2 = new Person("Mike", "Bloggs", 30, "2 Another Street");
Person p3 = new Person("Joe", "Bloggs", 29, "1 Elm Street");

people.Add(p1);
people.Add(p2);
people.Add(p3);

Now I would like to iterate through the list and find any duplicates. The criteria for a duplicate is the same age and the same surname - the address and forename is irrelevant.

I can do this with looping but think that using the generic List's .Find() method would be more elegant. Unfortunately any examples (including MSDN) are not clear where the matching criteria is passed in.

Any advice greatly appreciated.

Thanks,

Graeme

PS - I know the scenario I've presented is a bit nonsensical so please don't ask why I would want to find all 'Bloggs' that are same age!

"Just beacuse you're paranoid, don't mean they're not after you
 
This is a bit raw, but I think it'll get you started. Basically you need to have a boolean set up already to do your comparison (in this example called "Check"). I think that overriding the "Equals" method in your Person class makes this much easier (and limits change to the Person class, if your basis for comparision is to change).

Code:
    class Program
    {
        private static String _searchName;
        private static Int16 _searchAge;

        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a name to search for: ");
            _searchName = Console.ReadLine();
            Console.WriteLine("And the age to search for:");
            _searchAge = Int16.Parse(Console.ReadLine());

            List<Person> pList = new List<Person>();

            pList.Add(new Person("Alex", 26));
            pList.Add(new Person("George", 29));
            pList.Add(new Person("Alex", 16));
            pList.Add(new Person("Jeff", 23));
            pList.Add(new Person("Sean", 35));
            pList.Add(new Person("Alex", 26));
            pList.Add(new Person("Steve", 37));

            List<Person> tList = pList.FindAll(Check);

            foreach (Person per in tList)
                Console.WriteLine(per.ToString());
            
            Console.ReadLine();
        }

        static bool Check(Person p)
        {
            return p.Equals(new Person(_searchName, _searchAge));
        }


        class Person
        {
            private String _name;
            private Int16 _age;

            public Person()
            {
            }

            public Person(String name, Int16 age)
            {
                _name = name;
                _age = age;
            }

            public String Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public Int16 Age
            {
                get { return _age; }
                set { _age = value; }
            }

            public override bool Equals(object obj)
            {
                return ((Person)obj).Name == this.Name && ((Person)obj).Age == this.Age;
            }

            public override string ToString()
            {
                return this.Name + " is now " + this.Age.ToString();
            }

        }
    }

Hope it helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Alex,

Think this is exactly what I'm after! I'll implement later and post back.

Thanks,

Graeme


"Just beacuse you're paranoid, don't mean they're not after you
 
Chrissie's right, if you are using 2008, there are quite a few ways you could do this with Linq. Take a look at the Extension Methods on this page:
I assumed you were not using 2008, you know what happens when we do that ;)

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Alex,

Works exactly as I hoped - have a star!

Unfortunately, I'm using 2.0 so LINQ isn't an option.

Thanks,

Graeme


"Just beacuse you're paranoid, don't mean they're not after you
 
Yeah, not an option for me either.

By 2012 or so it should be ;-)

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top