views:

151

answers:

4

I have a question about this question. I posted a reply there but since it's been marked as answered, I don't think I'll get a response to my post there.

I am running C# framework 2.0 and I would like to get some of the data from a list? The list is a List<>. How can I do that without looping and doing comparaison manually on each element of the List<>?

It really looks like the answers are just a more elegant ways of comparing every element of the List. Given that the list is not guaranteed to be sorted prior to the search, do any of the methods provided in the original post ensure that they are looking at a smaller subset of the original list?

EDIT: One thing to note is that I'm not trying to do anything here. I just want to know if the solutions provided in another question truly do what the OP asked, with regards to looping through the whole list. In general, to search an unsorted list (at least it's not required given the data structure), you will have to search the entire list. However, do any of the solutions on the other thread have an underlying optimization to prevent searching the entire list?

EDIT: I really didn't get any answers that were all that helpful but I will give credit to the answer that at least confirmed my common sense belief. If I notice a new answer that is better, I will change my vote.

A: 

If you're only looking for the first match, then the Find method will do the job. It won't loop through the entire list, rather it will return the first occurrence of the object. However, if you want to find all of them, how exactly do you expect to search through only a subset of the data if it isn't sorted?

BFree
Your question is the underlying theme to mine -- do any of the methods others suggested sort the list prior to searching?
Austin Salonen
No, they don't. You have to explicitly sort it and for example use binary search to find an item. That way only some of the items will be touched.
liggett78
@liggett78: You should post that as an answer.
Austin Salonen
+1  A: 

You might want to check out LINQ support for .Net 2.0.

Jim Anderton
+2  A: 

If your requirement is to find things quickly in an arbitrary collection, then perhaps a list isn't the best data structure for the job. :)

Gabriel Isenberg
That's right. If your data is in a list and is unsorted how can you find something without enumerating the items in the list?
Andrew Kennan
+1  A: 

Has explain in the thread your mentionned you can get some of the object from the list without LINQ.

 list = list.FindAll(yourFilterCriteria);

The object yourFilterCriteria is a Predicate and can do comparison with all Property or Function in your object so it's very customizable.

    Predicate<SimpleObject> yourFilterCriteria = delegate(SimpleObject simpleObject)
    {
        return simpleObject.FirstName.Contains("Skeet") && simpleObject.Age < 30;
    };

This example show you that you can search the list without looping manullay and you will get all people with the First Name Skeet and Age under 30.

Daok