How to Get a Sublist in C#
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)
Is that what you're after?
If you're looking to delete the sublist items from the original list, you can then do:
// list is our original list
// sublist is our (newly created) sublist built from GetRange()
foreach (Type t in sublist)
{
list.Remove(t);
}
Would it be as easy as running 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();
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 statements above "p" is the object that is in the list. So if you used a data object, i.e.:
public class Client
{
public string Name { get; set; }
}
then your linq would 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");