Select and SelectMany with Example C# LINQ

In this tutorial, you will learn:
  1. What are SELECT and SELECTMANY Operators in Linq?
  2. Learn Select and SelectMany Operator with Programming Example

Select and SelectMany, both are projection operator, that means, it selects value from the list, collection or other source.

Select operator selects values from a collection whereas SelectMany Operator selects values from multiple collection or nested collection. SelectMany Operator selects values from multiple or nested collection and flatten the result.

You can understand Select and SelectMany Operator in LINQ more clearly when you will see the programming example.

Select Operator Example

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

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
        public List<string> Size { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var result = from p in GetProductDetails()
                         select new { p.productName, p.productPrice, p.Size };
            foreach (var r in result)
            {
                Console.WriteLine(r);
            }
            Console.ReadKey();
        }

        //Creating List of Product
        static List<ProductStore> GetProductDetails()
        {
            List<ProductStore> product = new List<ProductStore>
            {
            new ProductStore
                {
                productName = "HardDisk",
                productPrice = 3400,
                Size = new List<string>{"240GB","500GB","1TB"}
                },
            new ProductStore
                {
                productName = "RAM",
                productPrice = 7500,
                Size = new List<string>{"4GB","8GB","16GB"}
                },
            new ProductStore
                {
                productName = "Monitor",
                productPrice = 3400,
                Size = new List<string>{"14.5 Inch","18 Inch","24 Inch"}
                }
            };
            return product;
        }
    }
}

Output:


{ productName = HardDisk, productPrice = 3400, Size = System.Collections.Generic.List`1[System.String] }
{ productName = RAM, productPrice = 7500, Size = System.Collections.Generic.List`1[System.String] }
{ productName = Monitor, productPrice = 3400, Size = System.Collections.Generic.List`1[System.String] }
_

Explanation

If you’ll read output carefully, you will notice that the Size is not displayed correctly. At the place of Size, the program is displaying System.Collections.Generic.List. This is because, Size is a nested list and in order to display it properly, you need to implement SelectMany Operator.

SelectMany Operator Example

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

namespace LinqTutorial
{
    class ProductStore
    {
        public string productName { get; set; }
        public int productPrice { get; set; }
        public List<string> Size { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var result = from p in GetProductDetails()
                         select p;
            foreach (var r in result.SelectMany(ProductStore => ProductStore.Size))
            {
                Console.WriteLine(r);
            }
            Console.ReadKey();
        }

        //Creating List of Product
        static List<ProductStore> GetProductDetails()
        {
            List<ProductStore> product = new List<ProductStore>
            {
            new ProductStore
                {
                productName = "HardDisk",
                productPrice = 3400,
                Size = new List<string>{"240GB","500GB","1TB"}
                },
            new ProductStore
                {
                productName = "RAM",
                productPrice = 7500,
                Size = new List<string>{"4GB","8GB","16GB"}
                },
            new ProductStore
                {
                productName = "Monitor",
                productPrice = 3400,
                Size = new List<string>{"14.5 Inch","18 Inch","24 Inch"}
                }
            };
            return product;
        }
    }
}

Output

240GB
500GB
1TB
4GB
8GB
16GB
14.5 Inch
18 Inch
24 Inch
_

Summary

In the above example, I have used SelectMany Operator to print nested list value. This tutorial explains Select and SelectMany Operator in LINQ with the help of complete programming example. In the next chapter, you will learn OrderBy operator in C# LINQ.

 

Share your thought