How to get a sublist in C #

I have a List<String> , and I need to take subscriptions from this list. Are there any List methods for this in .NET 3.5?

+65
list c # sublist
Nov 11 '09 at 4:17
source share
6 answers

You want List :: GetRange (firstIndex, count). See http://msdn.microsoft.com/en-us/library/21k0e39c.aspx

 // I have a List called list List sublist = list.GetRange(5, 5); // (gets elements 5,6,7,8,9) List anotherSublist = list.GetRange(0, 4); // gets elements 0,1,2,3) 

This is what you need?

If you want to remove subscription items from the original list, you can:

 // list is our original list // sublist is our (newly created) sublist built from GetRange() foreach (Type t in sublist) { list.Remove(t); } 
+94
Nov 11 '09 at 4:22
source share

Would it be easier to run a LINQ query on your list?

 List<string> mylist = new List<string>{ "hello","world","foo","bar"}; List<string> listContainingLetterO = mylist.Where(x=>x.Contains("o")).ToList(); 
+5
Nov 11 '09 at 4:25
source share

Use the Where clause from LINQ:

 List<object> x = new List<object>(); x.Add("A"); x.Add("B"); x.Add("C"); x.Add("D"); x.Add("B"); var z = x.Where(p => p == "A"); z = x.Where(p => p == "B"); 

In the above expressions, "p" is an object that is in the list. Therefore, if you used a data object, that is:

 public class Client { public string Name { get; set; } } 

then your linq will look like this:

 List<Client> x = new List<Client>(); x.Add(new Client() { Name = "A" }); x.Add(new Client() { Name = "B" }); x.Add(new Client() { Name = "C" }); x.Add(new Client() { Name = "D" }); x.Add(new Client() { Name = "B" }); var z = x.Where(p => p.Name == "A"); z = x.Where(p => p.Name == "B"); 
+3
Nov 11 '09 at 4:26
source share

Your collection class may have a method that returns a collection (sublist) based on the criteria passed to define the filter. Create a new collection using the foreach loop and pass it.

Or, ask the method and the loop to modify the existing collection by selecting the filter or active (property) checkbox. This may work, but may also cause problems in multi-threaded code. If other objects depend on the contents of the collection, this is either good or bad, depending on how you use the data.

0
Nov 11 '09 at 4:28
source share

Discard items in a sub-list

 int[] l = {0, 1, 2, 3, 4, 5, 6}; var res = new List<int>(); res.AddRange(l.Where((n, i) => i < 2)); res.AddRange(l.Where((n, i) => i >= 2 && i <= 4).Reverse()); res.AddRange(l.Where((n, i) => i > 4)); 

Gives 0,1,4,3,2,5,6

0
Nov 11 '09 at 4:45
source share

With LINQ:

 List<string> l = new List<string> { "1", "2", "3" ,"4","5"}; List<string> l2 = l.Skip(1).Take(2).ToList(); 

If you need foreach then there is no need for ToList:

 foreach (string s in l.Skip(1).Take(2)){} 

The advantage of LINQ is that if you just want to skip any host element, you can:

 List<string> l2 = l.Skip(1).ToList(); foreach (string s in l.Skip(1)){} 

those. no need to care about quantity / length, etc.

0
May 02 '19 at 9:34
source share



All Articles