Csharp/CSharp Tutorial/LINQ/Range

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Range Cartesian Join

using System;
using System.ruponentModel;
using System.Linq;
    class MainClass
    {
        static void Main()
        {
            var query = from left in Enumerable.Range(1, 4)
                        from right in Enumerable.Range(11, left)
                        select new { Left = left, Right = right };
            foreach (var pair in query)
            {
                Console.WriteLine("Left={0}; Right={1}", pair.Left, pair.Right);
            }
        }
    }

Range Enumeration

using System;
using System.ruponentModel;
using System.Linq;
    class MainClass
    {
        static void Main()
        {
            var collection = Enumerable.Range(0, 10);
            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
        }
    }

Range Filtering

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ruponentModel;
    class MainClass
    {
        static void Main()
        {
            var collection = Enumerable.Range(0, 10)
                           .Where(x => x % 2 != 0)
                           .Reverse();
            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
        }
    }

Range Ordering

using System;
using System.ruponentModel;
using System.Linq;
    class MainClass
    {
        static void Main()
        {
            var collection = Enumerable.Range(-5, 11)
                                       .Select(x => new { Original = x, Square = x * x })
                                       .OrderBy(x => x.Square)
                                       .ThenBy(x => x.Original);
            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
        }
    }

Range Projection

using System;
using System.ruponentModel;
using System.Linq;
    class MainClass
    {
        static void Main()
        {
            var collection = Enumerable.Range(0, 10)
                                       .Where(x => x % 2 != 0)
                                       .Reverse()
                                       .Select(x => new { Original = x, SquareRoot = Math.Sqrt(x) });
            foreach (var element in collection)
            {
                Console.WriteLine("sqrt({0})={1}",
                                  element.Original,
                                  element.SquareRoot);
            }
        }
    }

Range Reversal

using System;
using System.ruponentModel;
using System.Linq;
    class MainClass
    {
        static void Main()
        {
            var collection = Enumerable.Range(0, 10).Reverse();
            foreach (var element in collection)
            {
                Console.WriteLine(element);
            }
        }
    }