3.4.1 使用过滤式扩展方法(P43-44)
对IEnumerable<T>执行标准并且同样返回IEnumerable<T>的扩展方法,可以使用yield关键字对源数据中的项应用选择标准,已生成精简的结果集。
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product>productEnum,string categoryParam)
{
foreach (Product prod in productEnum)
{
if (prod.Category == categoryParam)
{
yield return prod;
}
} }
使用
protected string GetMessage()
{ IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name ="Kayak",Category="Watersports",Price=275M},
new Product {Name ="Lifejacket",Category="Watersports",Price=48.95M},
new Product {Name ="Soccer ball",Category="Soccer",Price=19.5M},
new Product {Name ="Corner flag",Category="Soccer",Price=34.95M}
}
};
decimal total = products.FilterByCategory("Soccer").TotalPrices();
return String.Format("Soccer Total:{0:c}", total);
}
3.4.1 使用过滤式扩展方法(P43-44)的更多相关文章
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
- C#的扩展方法解析
在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...
- 扩展方法(C#)
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 下面的示例为String添加 ...
- 扩展方法解决LinqToSql Contains超过2100行报错问题
1.扩展方法 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- C#扩展方法
扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法就相当于一个马甲,给一个现有类套上,就可以为这个类添加其他方法了. 马甲必须定义为stati ...
- 枚举扩展方法获取枚举Description
枚举扩展方法 /// <summary> /// 扩展方法,获得枚举的Description /// </summary> /// <param name="v ...
- 扩展方法 1 简单的string扩展方法
这里是关于 String的简单扩展方法 (静态类 静态方法 this 类型 这里是string) static class Program { static void Main(string[] ar ...
随机推荐
- python bug the C library strftime function.
import timedef date2mktime(date, format_='%Y-%m-%d'): return int(time.mktime(time.strptime(date, for ...
- socketserver源码解析和协程版socketserver
来,贴上一段代码让你仰慕一下欧socketserver的魅力,看欧怎么完美实现多并发的魅力 client import socket ip_port = ('127.0.0.1',8009) sk = ...
- Linux下的pure-ftp的安装详解
FTP(File Transfer Protocol)是文件传输协议,常用于Internet上控制文件的双向传输.同时,它也是一个应用程序,用户可以通过它把自己PC机与世界各地所运行FTP协议的服务器 ...
- Linux学习-->如何通过Shell脚本实现发送邮件通知功能?
1.安装和配置sendmail 不需要注册公网域名和MX记录(不需要架设公网邮件服务器),通过Linux系统自带的mail命令即可对公网邮箱发送邮件.不过mail命令是依赖sendmail的,所以我们 ...
- ubuntu16.04 安装指定版本Node,升级npm到指定版本
一.安装配置Node 1.下载(64位系统) wget https://nodejs.org/download/release/v10.1.0/node-v10.1.0-linux-x64.tar.g ...
- Deep Learning(5)
五.应用实例 1.计算机视觉. ImageNet Classification with Deep Convolutional Neural Networks, Alex Krizhevsky, Il ...
- 使用jackson工具类把对象或集合转为JSON格式
jackson使用方法: 1.加入jar包: jackson-annotations-2.2.2.jar jackson-core-2.2.2.jar jackson-databind-2.2.2.j ...
- EWD简介
Edsger Wybe Dijkstra was a principal contributor in the late 1950's to the development of the ALGOL, ...
- C++ Builder创建和调用dll中的资源
程序开发中经常会用到一些图标.图片.光标.声音等,我们称它们为资源(Resource).当多个窗口用到同样的资源时,可以将这些公共的资源放到一个dll文件里调用,这样,由于定位资源比在磁盘中定位文件花 ...
- swoole gets
控制器调用: function gets() { $model = Model('ap_pic'); $model->select = ' id, size_type '; $gets['pag ...