Answers:
如果您使用的是C#3.0,则可以使用linq(更好,更优雅):
List<int> myList = GetListOfIntsFromSomewhere();
// This will filter out the list of ints that are > than 7, Where returns an
// IEnumerable<T> so a call to ToList is required to convert back to a List<T>.
List<int> filteredList = myList.Where( x => x > 7).ToList();
如果找不到.Where
,则意味着您需要using System.Linq;
在文件顶部导入。
.Where(predefinedQuery)
而不是使用.Where(x => x > 7)
?
public bool predefinedQuery(int x) { return x > 7; }
。这样您.Where(predefinedQuery)
就可以正常工作。
这是使用三种不同方法进行列表过滤的代码块/示例,我将它们组合在一起以展示基于Lambda和LINQ的列表过滤。
#region List Filtering
static void Main(string[] args)
{
ListFiltering();
Console.ReadLine();
}
private static void ListFiltering()
{
var PersonList = new List<Person>();
PersonList.Add(new Person() { Age = 23, Name = "Jon", Gender = "M" }); //Non-Constructor Object Property Initialization
PersonList.Add(new Person() { Age = 24, Name = "Jack", Gender = "M" });
PersonList.Add(new Person() { Age = 29, Name = "Billy", Gender = "M" });
PersonList.Add(new Person() { Age = 33, Name = "Bob", Gender = "M" });
PersonList.Add(new Person() { Age = 45, Name = "Frank", Gender = "M" });
PersonList.Add(new Person() { Age = 24, Name = "Anna", Gender = "F" });
PersonList.Add(new Person() { Age = 29, Name = "Sue", Gender = "F" });
PersonList.Add(new Person() { Age = 35, Name = "Sally", Gender = "F" });
PersonList.Add(new Person() { Age = 36, Name = "Jane", Gender = "F" });
PersonList.Add(new Person() { Age = 42, Name = "Jill", Gender = "F" });
//Logic: Show me all males that are less than 30 years old.
Console.WriteLine("");
//Iterative Method
Console.WriteLine("List Filter Normal Way:");
foreach (var p in PersonList)
if (p.Gender == "M" && p.Age < 30)
Console.WriteLine(p.Name + " is " + p.Age);
Console.WriteLine("");
//Lambda Filter Method
Console.WriteLine("List Filter Lambda Way");
foreach (var p in PersonList.Where(p => (p.Gender == "M" && p.Age < 30))) //.Where is an extension method
Console.WriteLine(p.Name + " is " + p.Age);
Console.WriteLine("");
//LINQ Query Method
Console.WriteLine("List Filter LINQ Way:");
foreach (var v in from p in PersonList
where p.Gender == "M" && p.Age < 30
select new { p.Name, p.Age })
Console.WriteLine(v.Name + " is " + v.Age);
}
private class Person
{
public Person() { }
public int Age { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
#endregion
List<T>
有一个FindAll
方法可以为您过滤并返回列表的子集。
MSDN在这里有一个很棒的代码示例:http : //msdn.microsoft.com/zh-cn/library/aa701359(VS.80).aspx
编辑:在我对LINQ和Where()
方法有一个很好的了解之前,我写了这篇文章。如果今天要写这篇文章,我可能会使用Jorge上面提到的方法。但是,FindAll
如果您陷在.NET 2.0环境中,该方法仍然有效。
您可以使用IEnumerable消除临时列表的需要。
public IEnumerable<T> GetFilteredItems(IEnumerable<T> collection)
{
foreach (T item in collection)
if (Matches<T>(item))
{
yield return item;
}
}
其中Matches是您的过滤方法的名称。您可以像这样使用:
IEnumerable<MyType> filteredItems = GetFilteredItems(myList);
foreach (MyType item in filteredItems)
{
// do sth with your filtered items
}
在需要时,这将调用GetFilteredItems函数,在某些情况下,如果您不使用过滤后的集合中的所有项目,则可能会获得一些良好的性能提升。