Select Linq Example

The Select operation can be thought of as selecting those properties or things that you are interested in. Another way of thinking of it is that you want to produce a projection on a list.

For instance, you may have a list of objects and you want to only extract a list of ids. In this case, you would want to project a list of these ids.

A basic example

We have a list of people below and we want to only extract the ids. Here, we can use the Select method and the lambda expression person => person.Id.

Live example: https://dotnetfiddle.net/OAryef

Method syntax

var people = new[]
{
   new {Id = 6000, Name = "Jennifer", Age = 21}, 
   new {Id = 6001, Name = "Alton", Age = 22},
   new {Id = 6003, Name = "Milford", Age = 23}
};

var ids = people.Select(person => person.Id);

//Output: 6000, 6001, 6003

Query syntax

Using the query syntax, a lambda expression is not needed here. Take note, it's the last select statement that is just needed select person.Id.

Live example: https://dotnetfiddle.net/NMbttZ

var people = new[]
{
   new {Id = 6000, Name = "Jennifer", Age = 21}, 
   new {Id = 6001, Name = "Alton", Age = 22},
   new {Id = 6003, Name = "Milford", Age = 23}
};

var ids = from person in people
          select person.Id

//Output: 6000, 6001, 6003

Select and map to another object

There may be times you will have a list of rich objects. Rich meaning that the objects have a lot of properties. In these instances, you may only need a condensed version of the object as only two or three properties are of interest to you. In this case, we can use the select method and just those properties to another new object.

Method syntax

Here we have an anonymous person profile with many properties such as Id, Name, Age, and so on. If we want to only project the id and name, we can do so as like so.

Live example: https://dotnetfiddle.net/GD2owR

var people = new[]
{
    new {Id = 6000, Name = "Jennifer", Age = 21, City="London", Country = "United Kingdom", UserName = "jen123"}, 
    new {Id = 6001, Name = "Alton", Age = 22, City="Paris", Country = "France", UserName = "alton_minton"},
    new {Id = 6003, Name = "Milford", Age = 23, City="New York", Country = "United States", UserName = "53milford"}
};

var condensed = people.Select(person => new { Id = person.Id, Name = person.Name});

// output: 
{ Id = 6000, Name = Jennifer }
{ Id = 6001, Name = Alton }
{ Id = 6003, Name = Milford }

Query syntax

Very similar to the method syntax above, the key statement is in the last select statement select new {Id = p.Id, Name = p.Name}.

Live example: https://dotnetfiddle.net/JUERU2

var people = new[]
{
    new {Id = 6000, Name = "Jennifer", Age = 21, City="London", Country = "United Kingdom", UserName = "jen123"}, 
    new {Id = 6001, Name = "Alton", Age = 22, City="Paris", Country = "France", UserName = "alton_minton"},
    new {Id = 6003, Name = "Milford", Age = 23, City="New York", Country = "United States", UserName = "53milford"}
};

var condensed = from p in people
                select new {Id = p.Id, Name = p.Name};

Select with an Index

It may not be very often you do this, but on the occasion, you may want to have an index or incrementing number on each object in your array or list. If this is the case, you can use the Select method with an index.

Method syntax

For this example, we have to use the select method that takes a Func with two parameters. The first parameter is the object that is iterated over the list, and the second is the index: (person, index) =>

Then we can simply create an anonymous object type with the index and create a new property called Order.

Live example: https://dotnetfiddle.net/1JDHDo

var people = new[]
 {
     new
         {
             Name = "Vernon",
             Age = 21,
             Email = "[email protected]",
             Phone = "806-291-8721"
         },
     new
         {
             Name = "Carrie",
             Age = 22,
             Email = "[email protected]",
             Phone = "617-389-2329"
         },
     new
         {
             Name = "Thomas",
             Age = 23,
             Email = "[email protected]",
             Phone = "906-875-5259"
         }
 };

var indexedPeople = people.Select((person, index) => new { Order = index, 
                                                           Name = person.Name, 
                                                           Email = person.Email });

// output:
// 0 Vernon [email protected]
// 1 Carrie [email protected]
// 2 Thomas [email protected]

Query syntax

With the query syntax, there isn't a handy function we can use. We will have to use an external counter such as index and increment as we go.

Live example: https://dotnetfiddle.net/l0dV28

var people = new[]
 {
     new
         {
             Name = "Vernon",
             Age = 21,
             Email = "[email protected]",
             Phone = "806-291-8721"
         },
     new
         {
             Name = "Carrie",
             Age = 22,
             Email = "[email protected]",
             Phone = "617-389-2329"
         },
     new
         {
             Name = "Thomas",
             Age = 23,
             Email = "[email protected]",
             Phone = "906-875-5259"
         }
 };

var index = 0;
var indexedPeople = from person in people
            select new { 
                            Name = person.Name, 
                            Email = person.Email, 
                            Order = index++  
                        };