Follow-up: How to get part of an object from a list without Linq?

I have a question about this question . I posted an answer there, but since it was flagged as an answer, I don't think I will answer my post.

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

Apparently the answers are just more elegant ways to compare each element of the list. Given that the list cannot be sorted prior to searching, would you do any of the methods mentioned in the original post so that they look at a smaller subset of the original list?

EDIT: It should be noted that I am not trying to do anything here. I just want to know if the solutions provided in the other question actually do what the OP asked in regards to iterating over the entire list. In general, to search for an unsorted list (at least not required given the data structure), you have to search the entire list. However, does any of the other thread's solutions have basic optimization to prevent searching the entire list?

EDIT: I didn't really have any answers that were helpful, but I'll give credit to an answer that at least confirms my common sense. If I notice a new answer that is better, I will change my voice.

-2


source to share


4 answers


If you are only looking for the first match, then the Find method will do the job. It will not iterate over the entire list, but return the first occurrence of the object. However, if you want to find all of them, how exactly do you expect to search only a subset of the data if it is not sorted?



0


source


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



+2


source


You might want to check LINQ Support for .Net 2.0 .

+1


source


Explain in the thread you talked about, you can get part of an object from a list without LINQ.

 list = list.FindAll(yourFilterCriteria);

      

The yourFilterCriteria object is a Predicate and can do comparisons to an entire property or function in your object, so it is very customizable.

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

      

This example shows that you can search a list without a manullay loop and you will get all people named Skeet and Age up to 30 years old.

+1


source







All Articles