вторник, 24 июня 2014 г.

Query Expression Syntax for Standard Query Operators

http://msdn.microsoft.com/ru-ru/library/bb882642.aspx

            class Pet
            {
                public string Name { get; set; }
                public int Age { get; set; }
            }

            public static void AllEx()
            {
                // Create an array of Pets.
                Pet[] pets = { new Pet { Name="Barley", Age=10 },
                               new Pet { Name="Boots", Age=4 },
                               new Pet { Name="Whiskers", Age=6 } };

                // Determine whether all pet names 
                // in the array start with 'B'.
                bool allStartWithB = pets.All(pet =>
                                                  pet.Name.StartsWith("B"));

                Console.WriteLine(
                    "{0} pet names start with 'B'.",
                    allStartWithB ? "All" : "Not all");
            }

            // This code produces the following output:
            //
            //  Not all pet names start with 'B'. 

   List<int> numbers = new List<int> { 1, 2 };
                bool hasElements = numbers.Any();

                Console.WriteLine("The list {0} empty.",
                    hasElements ? "is not" : "is");
 Clump<string> fruitClump =
                new Clump<string> { "apple", "passionfruit", "banana", 
                    "mango", "orange", "blueberry", "grape", "strawberry" };
IEnumerable<string> query2 =
                fruitClump.AsEnumerable().Where(fruit => fruit.Contains("o"));

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };
double average = grades.Average();
            public static void OrderByEx1()
            {
                Pet[] pets = { new Pet { Name="Barley", Age=8 },
                               new Pet { Name="Boots", Age=4 },
                               new Pet { Name="Whiskers", Age=1 } };

                IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);

            }
            string[] fruits = { "apple", "banana", "mango", "orange", 
                                  "passionfruit", "grape" };

            var query =
                fruits.Select((fruit, index) =>
                                  new { index, str = fruit.Substring(0, index) });

            int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

            IEnumerable<int> lowerGrades =
                grades.OrderByDescending(g => g).Skip(3);

   
            int[] amounts = { 5000, 2500, 9000, 8000, 
                                6500, 4000, 1500, 5500 };

            IEnumerable<int> query =
                amounts.SkipWhile((amount, index) => amount > index * 1000);

            int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

            IEnumerable<int> topThreeGrades =
                grades.OrderByDescending(grade => grade).Take(3);

string[] fruits = { "apple", "passionfruit", "banana", "mango", 
                                  "orange", "blueberry", "grape", "strawberry" };

            IEnumerable<string> query =
                fruits.TakeWhile((fruit, index) => fruit.Length >= index);
string[] fruits = { "grape", "passionfruit", "banana", "mango", 
                                  "orange", "raspberry", "apple", "blueberry" };

            // Sort the strings first by their length and then 
            //alphabetically by passing the identity selector function.
            IEnumerable<string> query =
                fruits.OrderBy(fruit => fruit.Length).ThenBy(fruit => fruit);
  int[] numbers = { 0, 30, 20, 15, 90, 85, 40, 75 };

            IEnumerable<int> query =
                numbers.Where((number, index) => number <= index * 10);

Комментариев нет:

Отправить комментарий