Return new objects from select and output in a for loop in CSharp

Description

The following code shows how to return new objects from select and output in a for loop.

Example


    /*from   w ww  . j  a v  a  2 s .co  m*/

using System;
using System.ComponentModel;
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);
            }
        }
    }

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ