三、查询集合

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表达式增强了可读性)。

最新文章

  1. 选择排序-java
  2. Python基础:序列(列表、元组)
  3. .NET破解之谷歌地图下载助手-睿智版
  4. 一、Oracle分析函数入门
  5. 远程ubuntu虚拟机Tensorflow搭建 - 1 SSH连接
  6. RAC 的一些概念性和原理性的知识(转)
  7. 开源 iOS 项目分类索引大全
  8. [SQL基础教程] 3-2 对表进行分组
  9. keystone V3 与Microsoft Active Directory(AD)的集成
  10. Linux下 Apache Vhost 配置 防止403
  11. ArcEngine小问题解决
  12. hydra用法
  13. Ubuntu18.04安装netstat
  14. SpringBoot项目部署在同一个tomcat容器报错
  15. LeetCode(867)
  16. Java多线程:多线程基础知识
  17. spring security 实践 + 源码分析
  18. 弹出序列(python)
  19. 【微信小程序】——rpx、px、rem等尺寸间关系浅析
  20. Uva 10559 消除方块

热门文章

  1. django2.0模板相关设置
  2. 好记性不如烂笔头--linux学习笔记8关于nginx的动静分离
  3. 字符串转码 将文本转为PDF
  4. excel拼接数据宏
  5. C++全总结
  6. python 安装pyqt4
  7. 2017面向对象程序设计(Java)第十一周学习总结
  8. Selenium+TestNG+Maven 搭建
  9. nginx反向代理同一主机多个网站域名
  10. 110. Balanced Binary Tree (Tree; DFS)