dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: How to search on properties of object in List<T>?

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    How to search on properties of object in List<T>?

    I have a List<T> of objects "Student". One of the many properties of Student is Student.SSN.

    Given findSSN = "555-77-9999"
    How do I search List<Student> to see if any Student.SSN in the list is equal to findSSN?

    This is with C#/.Net 4

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: How to search on properties of object in List<T>?

    It is merely a simple loop. (So I guess I am a bit confused about why you are asking... is there something that is specifically confusing you about how this works? Perhaps we could clear up your confusion; this solution should be exceeding obvious).

    Code:
    for(int i = 0; i < studentList.Count; i++)
    {
        if( studentList[i].SSN == givenSSN )
            //Do something
    }
    I'm sure there's some LINQ cuteness you could do, but I haven't gotten around to using those features with any regularity. Another may comment.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to search on properties of object in List<T>?

    Since you asked for a LINQ method as well...

    Code:
    string findSSN = "555-77-9999";
    var targetStudent = studentList.FirstOrDefault( s => s.SSN.Equals( findSSN ) );
    if( targetStudent != null )
    {
        // found a match
    }
    Since it is safe to assume that the SSN will be unique I used the FirstOrDefault method which will return the first match or null if the predicate does not return true for any element of the collection (First() will throw an exception if no match is found).

  4. #4
    Join Date
    Apr 2011
    Posts
    2

    Re: How to search on properties of object in List<T>?

    Thanks. I didn't realize that list objects could be referenced by an index like an array-- I was looking for a list method like Student.Contains, but there is no way to pass in findSSN with a predicate.

    I don't yet understand the syntax of the LINQ example (or about IEnumerables for that matter), but you've given me a starting point. Thank you.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to search on properties of object in List<T>?

    It's pretty simple really, you just need to look into lambda syntax. This part:

    Code:
    s => s.SSN.Equals( findSSN )
    Is simply a function that takes one argument of the same type as the list's generic argument (i.e., whatever the collection contains, in this case, Student objects) and returns a boolean. It is for all intents and purposes shorthand for a method with the following sugnature/semantics.

    Code:
    bool SomePredicate( Student s )
    {
        string findSSN = "555-77-9999";
        return s.SSN.Equals( findSSN );
    }
    Obviously not a very useful method in that form, but the method will be run for every element in the collection until a match is found. When the lambda function returns true, execution stops and the current Student argument is passed back as the result.

    Perhaps I can simplify further: the FirstOrDefault method says "give me a function that returns a boolean and I will execute it for every item in my collection. When the function returns true I will stop and return the object to you. If the function returns false for every item I return null".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)