void Main()
{
// This uses a custom 'Pair' extension method, defined below. Customers
.Select (c => c.Name.ToUpper())
.Pair() // Local from this point on.
.OrderBy (n => n)
.Dump(); // Here's a more substantial example: Customers
.Select (c => c.Name.ToUpper())
.OrderBy (n => n)
.Pair() // Local from this p.oint on.
.Select ((n, i) => "Pair " + i.ToString() + " = " + n)
.Dump(); Customers.Select (c => c.Name.ToUpper()).Add().Dump();
} public static class MyExtensions
{
public static IEnumerable<string> Pair (this IEnumerable<string> source)
{
string firstHalf = null;
foreach (string element in source)
if (firstHalf == null)
firstHalf = element;
else
{
yield return firstHalf + ", " + element;
firstHalf = null;
}
} public static IEnumerable<string> Add (this IEnumerable<string> source)
{
int index=;
foreach (string element in source)
{
yield return "NO."+index+++" " + element;
} }
}

IEnumerable扩展的更多相关文章

  1. 为IEnumerable扩展一个ForEach方法

    IEnumerable没有一个ForEach方法,我们可以使用C#写一个扩展方法: Source Code: using System; using System.Collections.Generi ...

  2. C# DataTable,DataSet,IList,IEnumerable 互转扩展属性

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  3. 【细语】C#之扩展方法原理及其使用

    1.写在前面 今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后(这个问题会在文章结尾回答).所以写了这边文章,力图从原理角度解释扩展方法及其使用. 以下为主要内容 ...

  4. 状态机/迭代器/LINQ/协程

    状态机 有限状态机(Finite State Machine 或 Finite State Automata)是软件领域中一种重要的工具. 状态机允许一个对象在其内部状态改变时改变它的行为.对象内部状 ...

  5. .net 温故知新:【6】Linq是什么

    1.什么是Linq 关于什么是Linq 我们先看看这段代码. List<int> list = new List<int> { 1, 1, 2, 2, 3, 3, 3, 5, ...

  6. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  7. IEnumerable<T>与IQueryable<T>以及.net的扩展方法

    首先看看继承关系 public abstract class DbSet : DbQuery public abstract class DbQuery : IOrderedQueryable, IQ ...

  8. 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列

    假如我有两个集合: public class Teacher { public int Id { get; set; } public string Name { get; set; } } publ ...

  9. IEnumerable接口的扩展方法

    /// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...

随机推荐

  1. Java笔记8:Hibernate连接Oracle

    1下载hibernate-3.6.0 Final.zip到任意目录,解压缩后得到hibernate目录 2下载slf4j-1.7.13.zip到任意目录,解压缩后得到slf4j-1.7.13 3操作数 ...

  2. 通过web php 执行shell脚本,获取的结果与直接在命令行下获取的结果不同。

    公司项目中的一项小功能,统计设备的连接数.其中用到shell脚本来获取已连接设备的统计.使用命令 /bin/netstat -an| grep ESTABLISHED | awk '{print $4 ...

  3. HTML 超级链接详细讲解

    超级链接 超级链接是网站中使用比较频繁的HTML元素,因为网站的各种页面都是由超级链接串接而成,超级链接完成了页面之间的跳转.超级链接是浏览者和服务器的交互的主要手段,在后面的技术中会逐步深化学习. ...

  4. [转]mysql 常用命令集锦[绝对精华]

    测试环境:mysql 5.0.45 [注:可以在mysql中通过mysql> SELECT VERSION();来查看数据库版本] 一.连接MYSQL. 格式: mysql -h主机地址 -u用 ...

  5. 如何设置outlook实现自动秘密抄送邮件的功能?

    很多朋友会发现虽然在家里同步了公司的邮箱可以正常收发邮件,可是每当使用家里的outlook发送相关邮件的时候,在公司的邮箱里找不到相关的发件记录,只能同步收件箱,而不能同步发件箱应该是比较让人困扰的问 ...

  6. odoo8登录时,同帐号在其它客户端的连接自动退出

    odoo跟大多数B/S系统一样,同一个帐号可以在不同的电脑上登录,并且可以同时操作,这样子会带来一些权限上的漏洞.为了解决这个问题,经过分析odoo的session处理机制,开发了一个模块,安装此模块 ...

  7. IOS后台文件上传

    public ModelAndView GetImage(HttpServletRequest request,   HttpServletResponse response) throws Exce ...

  8. 整站下载工具Teleport Pro

    http://zmingcx.com/download-tools-teleport-pro-full-stop.html Teleport Pro是一款功能强大的离线浏览器,不论规模多大的网站,只要 ...

  9. angularJS 状态样式绑定

    angularJS提供输入框不同状态下的样式绑定 输入框有4种状态 ng-model 指令可以为应用数据提供状态值(invalid, dirty, touched, error): <!DOCT ...

  10. pandas知识点汇总

    ## pandas基础知识汇总 1.时间序列 import pandas as pd import numpy as np import matplotlib.pyplot as plt from d ...