How to get part of an object from a list without Linq?

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

+3
source share
6 answers

You can try Predicate. Here is the code I wrote to illustrate the point. Of course, as you can see in this example, you can transfer the Predicate outside the calling class and have control over it. This is useful if you need to have more options. Inside the predicate, you can do a lot of comparison with the whole property / function of your object.

static void Main() { List<SimpleObject> list = new List<SimpleObject>(); list.Add(new SimpleObject(1,"Jon")); list.Add(new SimpleObject( 2, "Mr Skeet" )); list.Add(new SimpleObject( 3,"Miss Skeet" )); Predicate<SimpleObject> yourFilterCriteria = delegate(SimpleObject simpleObject) { return simpleObject.Name.Contains("Skeet"); }; list = list.FindAll(yourFilterCriteria);//Get only name that has Skeet : Here is the magic foreach (SimpleObject o in list) { Console.WriteLine(o); } Console.Read(); } public class SimpleObject { public int Id; public string Name; public SimpleObject(int id, string name) { this.Id=id; this.Name=name; } public override string ToString() { return string.Format("{0} : {1}",Id, Name); } } 
+3
source

If I follow your question correctly, you can simply call the Find() or FindAll() methods to get the data items from the list. For instance:

 List<string> myList = ..; List<string> startingWithA = myList.FindAll(delegate(string s) { return s.StartsWith("A"); }); 
+8
source

Without LINQ, your main parameter is to do a loop and a comparison for each item in the list. There are several ways that can help you.

List<T>.FindAll() accepts a Predicate delegate and returns all elements matching the condition. List<T>.CopyTo() and List<T>.GetRange() allow you to retrieve a number of elements. Other than this, you really cannot do much in the way of a specific choice outside LINQ.

+2
source

True, even if you end up using Predicates through Find or FindAll, everything it does inside iterates over the list and runs your Predicate to check for compliance. Performance is reasonable, you are not gaining anything, but it definitely does for cleaner code.

+1
source

Unfortunately, the List data structure requires iteration to find data (note that the FindAll methods described above will iterate your collection under covers β€” just in case you tried to avoid it at all costs), if you don’t know the index of this data, then you can do it:

 List<String> list = new List<String>(); list.Add("andrew"); list.Add("nicole"); String value = list[1]; // returns "nicole" 
0
source

I think most of the answers above just provide alternative ways to do exactly what you are trying to avoid. Given the data structure, no matter what implementation you choose, you / she will have to compare each element and choose the ones that match.

Depending on your matching criteria, SortedList can help you avoid searching the entire list for each request. If your list contains objects that you created, overriding GetHashCode and Equals will help you.

If the appropriate criteria are quite complex, you may need a different data structure, such as trie for strings.

0
source

All Articles