Finding objects in an array

D

Dylan Parry

Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
Person myPerson = people.GetThePersonWithName("Fred");

// Do things with "Fred"
...
}
----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
 
M

Marc Gravell

Array.Find:

Person fred = Array.Find<Person>(people, delegate(Person
person)
{
return person.Name == "Fred";
});

you might be able to drop the <Person> there... or even easier in C# 3
(still against .NET 2.0):

Person fred = Array.Find(people, person => person.Name ==
"Fred");

then:

if(fred != null)
{ // Do things with "Fred"
}

Marc
 
T

Terry Rogers

In C# 3.5:

People[] freds = Array.FindAll(people, p => p.FirstName == "Fred");
if(freds != null && freds.Length != 0)
{
// do things with freds
}

In C# 2.0:

People[] freds = Array.FindAll(people, delegate(Person p)
{
return p.FirstName == "Fred";
});
if(freds != null && freds.Length != 0)
{
// do things with freds
}

You could perform a contains check with Array.IndexOf first, but that
would involve 2 scans through the array.

Terry.
 
D

Dylan Parry

Dylan said:
Is there a simple way to do the above?
[...]

Thanks to all that replied. As I'm able to use 3.5, I've gone with the
LINQ solution. I did eventually come up with something similar myself,
but it was much more complicated and not at all elegant!

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
 
D

Duggi

Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
    Person myPerson = people.GetThePersonWithName("Fred");

    // Do things with "Fred"
    ...}

----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parryhttp://electricfreedom.org|http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.


C# 3.5

class Program
{
static void Main(string[] args)
{
List<Person> persons = GetPersons();

foreach (Person per in persons.Where(p => p.Name ==
"Fred"))
{
Console.WriteLine(per.Name);
}


Console.ReadLine();
}

public static List<Person> GetPersons()
{
List<Person> persons = new List<Person>();
persons.Add(new Person("Alex"));
persons.Add(new Person("Robert"));
persons.Add(new Person("Fred"));
persons.Add(new Person("Freak"));
persons.Add(new Person("Google"));
persons.Add(new Person("App"));

return persons;
}
}

This should work!!!
 
D

Duggi

Hi,

Say I have an array of objects of type "Person" (ie. Person[]) and each
Person object in the array has a property "Name" (ie. Person.Name), is
there a simple way of telling whether an object exists in the array that
has a specific value for the Name property?

This is what I want to do in pseudo-code:

----
Person[] people = myGetPeopleArrayMethod();

if (people.ContainsAPersonWithNameEquals("Fred"))
{
    Person myPerson = people.GetThePersonWithName("Fred");

    // Do things with "Fred"
    ...}

----

Is there a simple way to do the above? I don't necessarily want to
create all of those methods listed above, in fact if it can be done in
fewer steps, that would be even better.

Cheers,

--
Dylan Parryhttp://electricfreedom.org|http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.

C# 2.0

List<Person> persons = GetPersons();

Predicate<Person> pred = delegate(Person p)
{
return p.Name == "Fred";
};

List<Person> matches = persons.FindAll(pred);

-Cnu
 
M

Marc Gravell

C# 3.5

Pedant mode; there is no C# 3.5; that is C# 3 (from the lambda)
using .NET 3.5 - or actually it could be .NET 2.0 with LINQBridge or
any other similar LINQ-to-objects implementation. Either way, the
beauty of LINQ on IEnumerable<T> is that it will work even with the
OP's original array design (you don't need the List<T>).

Marc
 
D

Duggi

Pedant mode; there is no C# 3.5; that is C# 3 (from the lambda)
using .NET 3.5 - or actually it could be .NET 2.0 with LINQBridge or
any other similar LINQ-to-objects implementation. Either way, the
beauty of LINQ on IEnumerable<T> is that it will work even with the
OP's original array design (you don't need the List<T>).

Marc

Thanks for the info. I think you are right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top