Projection Operators


The Select operator performs a projection over a sequence


Select ........Enumerates the source sequence and yields the results of evaluating the selector function for each element.


SelectMany ....Performs a one-to-many element projection over a sequence.


When the object returned by Select is enumerated, it enumerates the source sequence and yields the results of evaluating the selector function for each element. The first argument of the selector function represents the element to process. The second argument, if present, represents the zero-based index of the element within the source sequence.


The following example creates a sequence of the names of all products:


IEnumerable<string> productNames = products.Select(p => p.Name);


When the object returned by SelectMany is enumerated, it enumerates the source sequence, maps each element to an enumerable object using the selector function, and enumerates the elements of each such enumerable object, yielding each if no resultSelector is given, or else passing each to the resultSelector along with the corresponding source element and yielding the resulting values. The first argument of the selector function represents the element to process. The second argument, if present, represents the zero-based index of the element within the source sequence.


The following example creates a sequence of the orders of the customers in Denmark:


IEnumerable<Order> orders = customers

.Where(c => c.Country == "Denmark")

.SelectMany(c => c.Orders);


Example


Select -

This sample prints a sequence of integers one greater than those in an input array. The sample uses the expression in the select clause to add one to each element in the new sequence.








using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication8

{

      class Program

      {

            static void Main(string[] args)

              {

                int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };


                var numsPlusOne =

                 from n in numbers

                  select n + 1;


                    Console.WriteLine("Numbers + 1:");

                     foreach (var i in numsPlusOne)

                      {

                       Console.WriteLine(i);

                      }

                      Console.ReadLine();



                }

        }

}



Output-


                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2012 VisualBuilder. All rights reserved