LINQ Projection Operators (Select, SelectMany)

The Linq projection operator is used to retrieve one or more from the tables. Its like select syntax of the standard SQL query. Select and Select many are two types of linq projection operators.

Linq projection operators

  • Select many
  • Select

Linq Select Operator

The select operator is used to retrieving data from the list or collection. Its same as SQL select query.

Syntax

var data = from s in user select u

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> user = new List<User>()
            {
                new User() { UserId = 1, Name = "Faisal", Age = 21 },
                new User() { UserId = 2, Name = "Irshad", Age = 18 },
                new User() { UserId = 2, Name = "Bhavdip", Age = 28 },
                new User() { UserId = 3, Name = "Kaushik", Age = 28 },
                new User() { UserId = 4, Name = "Kirti", Age = 25 }
            };
            var data = from s in user
                         select new {
                             Name = s.Name,
                             ID = s.UserId,
                             Age = s.Age
                         };
            foreach (var item in data)
            {
                Console.WriteLine("The User name is {0} ID is {1} Age is {2}", item.Name, item.ID, item.Age);
            }
            Console.ReadLine();
        }
        public class User
        {
            public int UserId { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

Here, we have select all the users from the list and store it in s variable and then created a new selection for the users

Output:

The User name is Faisal ID is 1 Age is 21
The User name is Irshad ID is 2 Age is 18
The User name is Bhavdip ID is 2 Age is 28
The User name is Kaushik ID is 3 Age is 28
The User name is Kirti ID is 4 Age is 25

Linq Select Many Operator

Select Many operator is used for selecting values from the collection of data of a collection of data i.e nested collection

Syntax

var data = user.SelectMany(m => m.Hobby);

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> user = new List<User>()
            {
                new User() { UserId = 1, Name = "Faisal", Age = 21, Hobby = new List<string> { "Coding", "Driving" } },
                new User() { UserId = 2, Name = "Irshad", Age = 18, Hobby = new List<string> { "Coding1", "Driving1" }  },
                new User() { UserId = 2, Name = "Bhavdip", Age = 28, Hobby = new List<string> { "Coding2", "Driving2" }  },
                new User() { UserId = 3, Name = "Kaushik", Age = 28, Hobby = new List<string> { "Coding3", "Driving3" }  },
                new User() { UserId = 4, Name = "Kirti", Age = 25, Hobby = new List<string> { "Coding4", "Driving4" }  }
            };
            var data = user.SelectMany(m => m.Hobby);
            foreach (var item in data)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
        public class User
        {
            public int UserId { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public List<string> Hobby { get; set; }
        }
    }
}

As you can see the SelectMany had written all the array of string otherwise we have to use the loop for accessing multiple properties.

Output:

Coding
Driving
Coding1
Driving1
Coding2
Driving2
Coding3
Driving3
Coding4
Driving4

 

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories