Dot Net For All

How to work with LINQ in DotNET Core

Hello friends, In this article I will show you how to work with LINQ in dotnet core framework released by Microsoft on Windows and Linux platform.

If you are working on the .NET framework you can directly add the reference of the “System.Linq” assembly in project and add the namespace.

Adding LINQ in dotnet core

In my previous article I have shown you how to create a .NET core project and build the project.

Once you are done with creating the project you can add the nuget package for the LINQ with the below command.

Adding LINQ nuget package on .NET Core

This is the screen is from the Ubuntu OS. But the command is same for the windows platform as well.

The command to execute in the CLI is

dotnet add package System.Linq

Once we install the package we can go and see in the .csproj file an item package is added.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Linq" Version="4.3.0" />   
  </ItemGroup>

</Project>

Now you need to add the reference the “System.Linq;” namespace to the .cs file.

Now try executing the below code:

using System;
using System.Linq;

namespace CSharpSampleProjects
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {1,2,3};

            var query = arr.Select(n => n * 10);

            foreach (var item in query)
            {
                Console.WriteLine(item);
            }
        }
    }
}

using the below command.

This is a very simple trick to work with LINQ queries on the DotNet Core platform on Linux as well as Windows platforms.

 

 

Master you .NET Core concepts

.NET core concepts and security

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview