Csharp/CSharp Tutorial/LINQ/Prototype

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

22.irst Select Prototype

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"A", "Ar", "Buc", "Bush", "Carte", "Clevel"};
        var nameObjs = presidents.Select(p => new { p, p.Length });
        foreach (var item in nameObjs)
            Console.WriteLine(item);
    }
}

22.econd Select Prototype

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"A", "B", "Bu", "Bush", "C", "Cl"};
        var nameObjs = presidents.Select((p, i) => new { Index = i, LastName = p });
        foreach (var item in nameObjs)
            Console.WriteLine("{0}. {1}", item.Index + 1, item.LastName);
    }
}

22.elect Prototype: string length

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"A", "Ar", "Buc", "Bush", "Carte", "Clevel"};
        var nameObjs = presidents.Select(p => new { LastName = p, Length = p.Length });
        foreach (var item in nameObjs)
            Console.WriteLine("{0} is {1} characters long.", item.LastName, item.Length);
    }
}

Where by Prototype

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"Ad", "Ar", "Bu", "Bu", "Ca", "Cl"};
        IEnumerable<string> sequence = presidents.Where((p, i) => (i & 1) == 1);
        foreach (string s in sequence)
            Console.WriteLine("{0}", s);
    }
}