Create new object in select statement in CSharp

Description

The following code shows how to create new object in select statement.

Example


    //from  w  w  w  .j a  v a2s . com

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

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ