三、查询集合
1.找出List<Product>列表中符合特定条件的所有元素
C#1.1 查询步骤:循环,if判断,打印
product类
using System.Collections;
using System.ComponentModel; namespace Chapter01.CSharp1
{
[Description("Listing 1.01")]
public class Product
{
string name;
public string Name
{
get { return name; }
} decimal price;
public decimal Price
{
get { return price; }
} public Product(string name, decimal price)
{
this.name = name;
this.price = price;
} public static ArrayList GetSampleProducts()
{
ArrayList list = new ArrayList();
list.Add(new Product("West Side Story", 9.99m));
list.Add(new Product("Assassins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
list.Add(new Product("Sweeney Todd", 10.99m));
return list;
} public override string ToString()
{
return string.Format("{0}: {1}", name, price);
}
}
}
ArrayListQuery类
using System;
using System.Collections;
using System.ComponentModel; namespace Chapter01.CSharp1
{
[Description("Listing 1.10")]
class ArrayListQuery
{
static void Main()
{
ArrayList products = Product.GetSampleProducts();
foreach (Product product in products)
{
if (product.Price > 10m)
{
Console.WriteLine(product);
}
}
}
}
}
2.测试和打印分开
C#2.0
product类
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp2
{
[Description("Listing 1.02")]
public class Product
{
string name;
public string Name
{
get { return name; }
private set { name = value; }
} decimal price;
public decimal Price
{
get { return price; }
private set { price = value; }
} public Product(string name, decimal price)
{
Name = name;
Price = price;
} public static List<Product> GetSampleProducts()
{
List<Product> list = new List<Product>();
list.Add(new Product("West Side Story", 9.99m));
list.Add(new Product("Assassins", 14.99m));
list.Add(new Product("Frogs", 13.99m));
list.Add(new Product("Sweeney Todd", 10.99m));
return list;
} public override string ToString()
{
return string.Format("{0}: {1}", name, price);
}
}
}
ListQueryWithDelegates类
using System;
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp2
{
[Description("Listing 1.11")]
class ListQueryWithDelegates
{
static void Main()
{
List<Product> products = Product.GetSampleProducts();
Predicate<Product> test = delegate(Product p)
{ return p.Price > 10m; };
List<Product> matches = products.FindAll(test); Action<Product> print = Console.WriteLine;
matches.ForEach(print);
}
}
}
变量test的初始化使用了匿名方法,而print变量的初始化使用了方法组转换,它简化了从现有方法创建委托的过程。不仅简单而且强大!
ListQueryWithDelegatesCompact类
using System;
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp2
{
[Description("Listing 1.12")]
class ListQueryWithDelegatesCompact
{
static void Main()
{
List<Product> products = Product.GetSampleProducts();
products.FindAll(delegate(Product p) { return p.Price > ; })
.ForEach(delegate(Product p) { Console.WriteLine(p); });
}
}
}
3.用lambda表达式来测试
C#3.0
product
using System.Collections.Generic;
using System.ComponentModel; namespace Chapter01.CSharp3
{
[Description("Listing 1.3")]
class Product
{
public string Name { get; private set; }
public decimal Price { get; private set; } public Product(string name, decimal price)
{
Name = name;
Price = price;
} Product()
{
} public static List<Product> GetSampleProducts()
{
return new List<Product>
{
new Product { Name="West Side Story", Price = 9.99m },
new Product { Name="Assassins", Price=14.99m },
new Product { Name="Frogs", Price=13.99m },
new Product { Name="Sweeney Todd", Price=10.99m}
};
} public override string ToString()
{
return string.Format("{0}: {1}", Name, Price);
}
}
}
ListQueryWithLambdaExpression类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; namespace Chapter01.CSharp3
{
[Description("Listing 1.13")]
class ListQueryWithLambdaExpression
{
static void Main()
{
List<Product> products = Product.GetSampleProducts();
foreach (Product product in products.Where(p => p.Price > ))
{
Console.WriteLine(product);
}
}
}
}
总结:
→C#1,条件和操作紧密耦合两者都是硬编码的
→C#2,条件和操作分开,匿名方法使委托变得简单(匿名方法有助于问题的可分离性)
→C#3Lambda表达式使条件变得更容易阅读(Lambda表达式增强了可读性)。