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

我是研究僧i 提交于 2019-12-10 02:42:57

问题


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


回答1:


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

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

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.




回答2:


The best solution is to use 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.




回答3:


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:

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:

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.




回答4:


besides the way told by @Razzie you can also use 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




回答5:


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

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;



回答6:


You can use LINQ like this.

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.



来源:https://stackoverflow.com/questions/897466/filter-list-object-without-using-foreach-loop-in-c2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!