在 C#2.0 中不使用 foreach 循环的 Filter List<> 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/897466/
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-05 22:04:06  来源:igfitidea点击:

Filter List<> object without using foreach loop in C#2.0

c#c#-2.0

提问by Kthevar

How we can filter the object in List<> in C#?

我们如何在 C# 中过滤 List<> 中的对象?

采纳答案by Razzie

Let's say we have a List<string>and you want only the items where the length of the string is greater than 5.

假设我们有 aList<string>并且您只想要字符串长度大于 5 的项目。

The code below will return a List<string>with the results:

下面的代码将返回一个List<string>结果:

List<string> myList = new List<string>();
myList.Add("hello");
myList.Add("world!");
myList.Add("one");
myList.Add("large!!");
List<string> resultList = myList.FindAll(delegate(string s) { return s.Length > 5; });

resultList will containt 'world!' and 'large!!'. This example uses an anonymous method. It can also be written as:

结果列表将包含“世界!” 和“大!!”。此示例使用匿名方法。也可以写成:

List<string> myList = new List<string>();
// ..
List<string> resultList = myList.FindAll(OnlyLargerThanFive);

//..

private static bool OnlyLargerThanFive(string s)
{
  return s.Length > 5;
}

The delegate above, OnlyLargerThanFive, is also called a Predicate.

上面的委托 OnlyLargerThanFive 也称为Predicate

回答by TheVillageIdiot

besides the way told by @Razzieyou can also use LINQ.

除了@Razzie告诉的方式之外,您还可以使用LINQ

List<string> myList = new List<string>();
myList.Add("hello");
myList.Add("world!");
myList.Add("one");
myList.Add("large!!");

var filtered=from s in myList where s.Length > 5 select s;

PS:- IS ONLY POSSIBLE IN .NET 3 and above

PS:-仅在 .NET 3 及以上版本中才有可能

回答by Migol

The best solution is to use lambda:

最好的解决方案是使用 lambda:

List<Item> l;
l.FindAll(n => n.Something == SomethingElse);

It may use internally foreach, but you can't really filter without iterating for whole list.

它可能会在内部使用 foreach,但如果不迭代整个列表,就无法真正进行过滤。

回答by itsmatt

You could use LINQ. I haven't tested this, but I believe it'll filter down the elements of my list of pie fillings to show only those that start with a "P":

你可以使用LINQ。我还没有测试过这个,但我相信它会过滤掉我的馅饼列表中的元素,只显示那些以“P”开头的元素:

List<string> list = new List<string>();

list.Add("Apple");
list.Add("Peach");
list.Add("Chocolate");
list.Add("Pear");
list.Add("Pumpkin");
list.Add("Cherry");
list.Add("Coconut");


var filteredOnes = from item in list
                   where item.StartsWith("P")
                   select item;

回答by SO User

List<>.Find (gives the first matching occurence) and List.FindAll() gives all matching occurences. An example with a list of complex types would is as follow:

List<>.Find(给出第一个匹配的出现)和 List.FindAll() 给出所有匹配的出现。具有复杂类型列表的示例如下:

I have a class Report:

我有一个班级报告:

public class Report
{
    public string ReportName;
    public ReportColumnList ReportColumnList;
}

and a list of Report

和一份报告清单

List<Report> reportList;

To find items in the list where ReportName = 'MyReport', the code would be:

要在列表中查找 ReportName = 'MyReport' 的项目,代码为:

string reportName = "MyReport";
List<Report> myReports = reportList.FindAll(delegate(Report obj) { return obj.ReportName == reportName; });

To get the first report:

获取第一份报告:

Report rc = reportList.Find(delegate(Report obj) { return obj.ReportName == reportName; });

Note that the object passed to the delegate should be of the type with which the list is populated.

请注意,传递给委托的对象应该是填充列表类型

回答by Yissachar

You can use LINQlike this.

您可以像这样使用LINQ

List<string> List = new List<string> { "i", "am", "using", "LINQ!" };List<string> result = myList.Where(s => s.Length > 3).ToList();

List<string> List = new List<string> { "i", "am", "using", "LINQ!" };List<string> result = myList.Where(s => s.Length > 3).ToList();

it's working only in .net 3 and above.

它仅适用于 .net 3 及更高版本。