How to Get a Sublist in C#

前端 未结 6 1548
走了就别回头了
走了就别回头了 2021-02-02 05:09

I have a List and i need to take a sublist out of this list. Is there any methods of List available for this in .NET 3.5?

相关标签:
6条回答
  • 2021-02-02 05:13

    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 no need for ToList:

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

    Advantage of LINQ is that if you want to just skip some leading element,you can :

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

    i.e. no need to take care of count/length, etc.

    0 讨论(0)
  • 2021-02-02 05:16

    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);
    }
    
    0 讨论(0)
  • 2021-02-02 05:20

    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();
    
    0 讨论(0)
  • 2021-02-02 05:27

    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");
    
    0 讨论(0)
  • 2021-02-02 05:34

    Reverse the 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 讨论(0)
  • 2021-02-02 05:38

    Your collection class could have a method that returns a collection (a sublist) based on criteria passed in to define the filter. Build a new collection with the foreach loop and pass it out.

    Or, have the method and loop modify the existing collection by setting a "filtered" or "active" flag (property). This one could work but could also cause poblems in multithreaded code. If other objects deped on the contents of the collection this is either good or bad depending of how you use the data.

    0 讨论(0)
提交回复
热议问题