Home
C#
select new Example
Updated Jun 22, 2021
Dot Net Perls
Select new. A C# query expression can create a new data structure. With the select new clause, we create new objects of an anonymous type as the result of a query.
object
C# notes. The "select new" keywords are contextual—the "new" keyword has other uses in the C# language. We use these keywords in a query expression (System.Linq is required).
new
First example. The select new clause creates an anonymous type instance. In this program, we set the Value and Id in each element—each new object will have a Value and Id.
Then You can loop over the AnonymousType instances in the query expression result. We use var to simplify the syntax.
var
Tip This query expression transformed the string array into a collection of objects with a Value and Id property.
Array
Property
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] array = { "one", "two", "three", "four", "", "seven" };

        // Use Select New.
        var result = from element in array
                     select new { Value = element, Id = Id(element) };

        foreach (var anonymous in result)
        {
            string value = anonymous.Value;
            int id = anonymous.Id;

            Console.WriteLine(anonymous);
        }
    }

    static int _value;
    static int Id(string element)
    {
        // Get Id.
        return string.IsNullOrEmpty(element) ?
            -1 :
            _value++;
    }
}
{ Value = one, Id = 0 } { Value = two, Id = 1 } { Value = three, Id = 2 } { Value = four, Id = 3 } { Value = , Id = -1 } { Value = seven, Id = 4 }
HTML. This example constructs HTML strings from a string array. In each anonymous type instance, an Html property and a Length property are present.
Tip For ideal performance when combining many strings together, consider a StringBuilder or a char array.
StringBuilder
char Array
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] items = { "cat", "dog", "mouse" };

        // Project items into HTML strings.
        var html = from item in items
                   select new { Html = "<p>" + item + "</p>", Length = item.Length };

        // Display all projected values.
        foreach (var value in html)
        {
            Console.WriteLine(value.Html);
            Console.WriteLine(value.Length);
        }
    }
}
<p>cat</p> 3 <p>dog</p> 3 <p>mouse</p> 5
A summary. With select new, we project elements into anonymous types. This syntax allows us to construct anonymous data structures. These are created as they are evaluated (lazily).
LINQ tips. You can avoid declaring classes to store this data entirely if you wish to. Classes clarify and improve programs, and anonymous types lack these positive features.
class
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 22, 2021 (rewrite).
Home
Changes
© 2007-2025 Sam Allen