IEnumerable扩展

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=1;
            foreach (string element in source)
            {
                yield return "NO."+index+++" " + element;
            }
    
        
    }
}

 

你可能感兴趣的:(IEnumerable扩展)