如何在 C# 中获取子列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1712975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:11:26  来源:igfitidea点击:

How to Get a Sublist in C#

c#.netlist.net-3.5sublist

提问by Thomas Manalil

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

我有一个List<String>,我需要从这个列表中取出一个子列表。在 .NET 3.5 中是否有任何可用的 List 方法?

采纳答案by Josh

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

你想要 List::GetRange(firstIndex, count)。请参阅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);
}

回答by p.campbell

Would it be as easy as running a LINQ query on your List?

它会像在您的列表上运行 LINQ 查询一样简单吗?

List<string> mylist = new List<string>{ "hello","world","foo","bar"};
List<string> listContainingLetterO = mylist.Where(x=>x.Contains("o")).ToList();

回答by slugster

Use the Where clause from LINQ:

使用 LINQ 中的 Where 子句:

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.:

在上面的语句中,“p”是列表中的对象。因此,如果您使用了数据对象,即:

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

then your linq would look like this:

那么您的 linq 将如下所示:

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");

回答by Doug L.

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.

您的集合类可以有一个方法,该方法根据传入的条件返回集合(子列表)以定义过滤器。使用 foreach 循环构建一个新集合并将其传递出去。

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.

或者,通过设置“过滤”或“活动”标志(属性)让方法和循环修改现有集合。这个可以工作,但也可能导致多线程代码出现问题。如果其他对象依赖于集合的内容,这取决于您如何使用数据。

回答by Phillip Ngan

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,1,4,3,2,5,6

回答by jw_

With LINQ:

使用 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,则不需要 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 :

LINQ 的优点是如果你只想跳过一些前导元素,你可以:

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.

即不需要照顾计数/长度等。