Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. <font face="Trebuchet MS">Use delegate to find distinct values for member variable from a list of objects</font>

<font face="Trebuchet MS">Use delegate to find distinct values for member variable from a list of objects</font>

Scheduled Pinned Locked Moved ASP.NET
design
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Gathogo
    wrote on last edited by
    #1

    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;
        }
    }
    
    P 1 Reply Last reply
    0
    • J John Gathogo

      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;
          }
      }
      
      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      Example here[^].

      J 1 Reply Last reply
      0
      • P Paddy Boyd

        Example here[^].

        J Offline
        J Offline
        John Gathogo
        wrote on last edited by
        #3

        Unfortunately your link does not address my question in any way.

        P 1 Reply Last reply
        0
        • J John Gathogo

          Unfortunately your link does not address my question in any way.

          P Offline
          P Offline
          Paddy Boyd
          wrote on last edited by
          #4

          Tremendously sorry, adapted my google search by one word: http://csharpaspnet.blogspot.com/2008/04/generics-list-to-filter.html[^]

          J 1 Reply Last reply
          0
          • P Paddy Boyd

            Tremendously sorry, adapted my google search by one word: http://csharpaspnet.blogspot.com/2008/04/generics-list-to-filter.html[^]

            J Offline
            J Offline
            John Gathogo
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups