<font face="Trebuchet MS">Use delegate to find distinct values for member variable from a list of objects</font>
-
Hi In the code block below, I am identifying distinct values for a member variable 'Age' of class Person from a list of persons List<Person> by looping. I wanted to know whether I can use a suitable delegate to accomplish the same without having to loop. See code comments. Thanks
public partial class Default1 : System.Web.UI.Page { protected void Page\_Load(object sender, EventArgs e) { List<Person> people = new List<Person>(); people.Add(new Person() { FirstName = "Gill", LastName = "Julian", Age = 20 }); people.Add(new Person() { FirstName = "Hill", LastName = "Andrew", Age = 19 }); people.Add(new Person() { FirstName = "Hart", LastName = "Brett", Age = 24 }); people.Add(new Person() { FirstName = "Dwight", LastName = "Jane", Age = 20 }); people.Add(new Person() { FirstName = "Gary", LastName = "Gray", Age = 18 }); people.Add(new Person() { FirstName = "Prim", LastName = "Anne", Age = 19 }); List<int> distinctAges = new List<int>(); //get a list of distinct ages from 'people' list foreach(Person person in people) if(!distinctAges.Contains(person.Age)) distinctAges.Add(person.Age); //wonder whether there is a delegate I can use to do the above //Like: distinctAges = people.FindAll( delegate ... } public class Person { public string FirstName; public string LastName; public int Age; } }
-
Hi In the code block below, I am identifying distinct values for a member variable 'Age' of class Person from a list of persons List<Person> by looping. I wanted to know whether I can use a suitable delegate to accomplish the same without having to loop. See code comments. Thanks
public partial class Default1 : System.Web.UI.Page { protected void Page\_Load(object sender, EventArgs e) { List<Person> people = new List<Person>(); people.Add(new Person() { FirstName = "Gill", LastName = "Julian", Age = 20 }); people.Add(new Person() { FirstName = "Hill", LastName = "Andrew", Age = 19 }); people.Add(new Person() { FirstName = "Hart", LastName = "Brett", Age = 24 }); people.Add(new Person() { FirstName = "Dwight", LastName = "Jane", Age = 20 }); people.Add(new Person() { FirstName = "Gary", LastName = "Gray", Age = 18 }); people.Add(new Person() { FirstName = "Prim", LastName = "Anne", Age = 19 }); List<int> distinctAges = new List<int>(); //get a list of distinct ages from 'people' list foreach(Person person in people) if(!distinctAges.Contains(person.Age)) distinctAges.Add(person.Age); //wonder whether there is a delegate I can use to do the above //Like: distinctAges = people.FindAll( delegate ... } public class Person { public string FirstName; public string LastName; public int Age; } }
-
Unfortunately your link does not address my question in any way.
-
Unfortunately your link does not address my question in any way.
Tremendously sorry, adapted my google search by one word: http://csharpaspnet.blogspot.com/2008/04/generics-list-to-filter.html[^]
-
Tremendously sorry, adapted my google search by one word: http://csharpaspnet.blogspot.com/2008/04/generics-list-to-filter.html[^]
Where there is a will, there is a way. When you declare a list (like List<Person>, in my example), you will need to add System.Collections.Generic namespace. Now, if you also add System.Linq namespace, you will get a couple more methods to use with your list that you would not get if you just added System.Collections.Generic namespace. These methods will allow you to do more with your list. Hence, to find distinct values for member variables based on my example below, the following statement will suffice.
distinctAges = people.Select(c => c.Age).Distinct().ToList();
That simple. The Select is not available for use until you add System.Linq namespace. The revised code will then be as follows: public partial class Default1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Person> people = new List<Person>();people.Add(new Person() { FirstName = "Gill", LastName = "Julian", Age = 20 }); people.Add(new Person() { FirstName = "Hill", LastName = "Andrew", Age = 19 }); people.Add(new Person() { FirstName = "Hart", LastName = "Brett", Age = 24 }); people.Add(new Person() { FirstName = "Dwight", LastName = "Jane", Age = 20 }); people.Add(new Person() { FirstName = "Gary", LastName = "Gray", Age = 18 }); people.Add(new Person() { FirstName = "Prim", LastName = "Anne", Age = 19 }); List<int> distinctAges = new List<int>(); ////get a list of distinct ages from 'people' list: looping //foreach(Person person in people) // if(!distinctAges.Contains(person.Age)) // distinctAges.Add(person.Age); //get a list of distinct ages from 'people' list: delegate distinctAges = people.Select(c => c.Age).Distinct().ToList(); } public class Person { public string FirstName; public string LastName; public int Age; } }
Thanks everyone