The other day I was using Underscore quite a bit while working on some Angular code. There are many helpful features like map, filter, and range. I then needed to do some C# changes on the server side and realized that C# has similar helpful features like Underscore does for working with collections of data. These helpful features are available because of LINQ.

Here is an example of how to use the .Select method on a collection in C#. What we can do is take a list of employees in a single company and iterate through them using the .Select method to return a list of their email addresses. First let’s make a list of employees:

var employees = new List<string>();
employees.Add("steve");
employees.Add("bob");
employees.Add("amy");

Now we can do some magic to return a list of emails using the .Select method:

var emails = employees.Select(x => x + "@example.com");

Now that we have our email list we can print them out to the console (or send emails, add them to a database table, etc.):

foreach (var email in emails)
{
    Console.WriteLine(email);
}

Below is the entire source for the Console Application we just built:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Select
{
    class Program
    {
        static void Main(string[] args)
        {
            var employees = new List<string>();
            employees.Add("steve");
            employees.Add("bob");
            employees.Add("jon");

            var emails = employees.Select(x => x + "@example.com");
            
            foreach (var email in emails)
            {
                Console.WriteLine(email);
            }
        }
    }
}