locked
Looping List of Objects + using LINQ to WHERE

    Question

  • Hi all, I was wondering how would one go about looping an array of objects and performing a LINQ query on this list of objects? Looking at the code below I want to say WHERE Person.Name == "some value":

    How could I perform a WHERE statement on age or on the name of a person using LINQ as imagine its quicker and better to use LINQ as opposed to an IF or SWITCH statement?

    Can someone also be kind enough to say why it is better to use LINQ than an IF or SWITCH statement? From what I gather LINQ offers relatively simple search facilities that SQL offers and maybe I should use it more often but would like to know whether its better to use it beause of memory, speed etc...

    Thanks, Onam.


            private void btn_ObjectLINQ_Click(object sender, EventArgs e) 
            { 
                List<object> People = new List<object>(); 
                People.Add(new Person("Person 1", 24)); 
                People.Add(new Person("Person 2", 12)); 
                People.Add(new Person("Person 3", 17)); 
                People.Add(new Person("Person 4", 46)); 
                             
                var qry = from p in People                      
                          select p; 
     
                foreach (object p in qry) 
                {                 
                    textBox1.Text += p.ToString() + Environment.NewLine; 
                } 
            }

            class Person 
            { 
                private String Str_Person_Name; 
                private int Int_Age; 
     
     
                public Person(String Name, int Age) 
                { 
                    this.Str_Person_Name = Name; 
                    this.Int_Age = Age; 
                } 
     
                public String getName 
                { 
                    get { return Str_Person_Name; } 
                } 
     
                public int getAge 
                { 
                    get { return Int_Age; } 
                }        
            } 



    • Edited by OnamC Tuesday, February 17, 2009 12:04 PM Changed post
    Tuesday, February 17, 2009 10:48 AM

Answers

  • Yes, and also if you prefer you can use the extension methods over the LINQ query syntax. I prefer the extension methods personally.

    Person p = People.Where(i => i.getName == "Person 1").First();

    Also, it is common practice in .NET to follow the guidelines of best practice. Properties are not two seperate methods as in Java's getters/setters. Use auto implemented properties if they are "regular" non checking properties.

    .NET
    public string Name { get; set; }

    Else if you have to check them use:

    private string name;

    public string Name
    {
          get { return name; }
          set { name = value; }
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Tuesday, February 17, 2009 6:28 PM `
    • Marked as answer by OnamC Thursday, February 19, 2009 12:01 PM
    Tuesday, February 17, 2009 3:57 PM

All replies

  • This has been mentioned before, code like this is not going to work until you stop using "object".  It has to be a List<People>.  Not sure, the damage done by ArrayList may be permanent.
    Hans Passant.
    Tuesday, February 17, 2009 12:40 PM
  • Oh I see so I would do something like this:

            private void btn_ObjectLINQ_Click(object sender, EventArgs e) 
            { 
                List<Person> People = new List<Person>(); 
                            
                People.Add(new Person("Person 1", 24)); 
                People.Add(new Person("Person 2", 12)); 
                People.Add(new Person("Person 3", 17)); 
                People.Add(new Person("Person 4", 46)); 
     
                var personQuery = from person in People 
                                  where person.getName == "Person 1" 
                                  select person; 
     
                foreach (Person person in personQuery) 
                { 
                    textBox1.Text = person.getName.ToString() + ", " + person.getAge.ToString(); 
                } 
            } 

    • Edited by OnamC Tuesday, February 17, 2009 1:17 PM updated code
    Tuesday, February 17, 2009 1:00 PM
  • Yes, and also if you prefer you can use the extension methods over the LINQ query syntax. I prefer the extension methods personally.

    Person p = People.Where(i => i.getName == "Person 1").First();

    Also, it is common practice in .NET to follow the guidelines of best practice. Properties are not two seperate methods as in Java's getters/setters. Use auto implemented properties if they are "regular" non checking properties.

    .NET
    public string Name { get; set; }

    Else if you have to check them use:

    private string name;

    public string Name
    {
          get { return name; }
          set { name = value; }
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Tuesday, February 17, 2009 6:28 PM `
    • Marked as answer by OnamC Thursday, February 19, 2009 12:01 PM
    Tuesday, February 17, 2009 3:57 PM
  • HI JohnGrove I see you are very active among these forums and maybe you could explain to me why is better (if it is better) to use properties? For instance, instead of using getters and setters can I not just declare the variables public in the class declaration instead of having to create a property to access them? I imagine that its safer to use properties but how are they made safe and why is it better to use a property as opposed to declaring the variable public?

    For instance, code below work the same but one uses properties but other doesn't:

    Using Properties:

        class Person 
        { 
            private String Str_Person_Name; 
            private int Int_Age; 
     
     
            public Person(string Name, int Age) 
            { 
                this.Str_Person_Name = Name; 
                this.Int_Age = Age; 
            } 
     
            public string getName 
            { 
                get { return Str_Person_Name; } 
            } 
     
            public int getAge 
            { 
                get { return Int_Age; } 
            }       
        } 


    Using publicly decalred variable:

        class Person 
        { 
            public String Str_Person_Name; 
            public int Int_Age; 
     
     
            public Person(string Name, int Age) 
            { 
                this.Str_Person_Name = Name; 
                this.Int_Age = Age; 
            } 
        } 

    Wednesday, February 18, 2009 10:45 AM
  • But doing it that way violates the numbr one rule in object oriented programming. And that is encapsulation.  Keeping details of a software routine (function or object) private, clients of that code only know what input is required and what outputs are expected.

    Read:

    Object Oriented Programming - Encapsulation is not just data hiding

     




    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, February 18, 2009 1:48 PM
  • Am I right in understanding from what you have said that using properties is better than declaring variables public?
    Wednesday, February 18, 2009 3:26 PM
  • OnamC said:

    Am I right in understanding from what you have said that using properties is better than declaring variables public?

    Let me recommend a book for you titled "The Object Oriented Thought Process" so you can build a foundation on the tenets of Object Orientation and abstraction. These topics need to be "sunk" in. So you need to read about, do examples and then you will start to comprehend the depth of OO techniques.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Wednesday, February 18, 2009 3:45 PM