Signup/Sign In

C# LINQ

Posted in Programming   LAST UPDATED: SEPTEMBER 9, 2021

    In this tutorial, we will be discussing one of the most powerful features of C# programming language: LINQ (Language-Integrated Query), which was added in C# 3.0. Through this feature, we can easily access the data from data sources using query expression. This article gives you an overview of how to use LINQ methods and query expressions to improve the readability of programs.

    Introduction to C# LINQ

    The LINQ stands for Language-Integrated Query, and it gives us an ability to retrieve the information from a data source such as SQL databases, Collections, XML documents, Web services, etc. In the past, retrieving/querying data from multiple data sources required knowledge of various technologies like SQL, XML, etc. But with the help of LINQ, we can easily retrieve the data using query processing, and with the help of this, we can perform filtering, ordering, and grouping operations on data sources.

    NOTE: LINQ is implemented as a set of extension methods on the IEnumerable<T> interface.

    Let’s take a basic example of LINQ query

    Filename: Program.cs

    using System;
    using System.Linq;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                string[] cities = { "Mumbai", "Delhi", "Hyderabad", "Chennai" };
                var query = from citynames in cities where citynames.Contains('i') select citynames;
                foreach(var city in query)
                {
                    Console.WriteLine(city);
                }
                Console.ReadKey();
            }
        }
    }


    Mumbai
    Delhi
    Chennai

    In the above example, we have created an array of city names with four elements in it. Furthermore, we wrote a query to get all the city names that contain the “i” alphabet in it. Lastly, with the help of the foreach loop, we have printed the desired result on the console.

    C# Query Expressions

    The query expression is a LINQ query that starts with “from” keyword. It is similar to SQL query operations such as SELECT, WHERE, and ORDERBY. The following example illustrates a complete query operation that consists of a data source, query expression, and execution.

    Filename: Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    namespace Studytonight
    {
        class Program
        {
            public static void Main(string[] args)
            {
                List<double> amount = new List<double>() { 99927.23, 125.00, 81889.36, 6000 };
                IEnumerable<double> savings = from amt in amount where amt >=10000 select amt;
                foreach(var a in savings)
                {
                    Console.WriteLine(a);
                }
                Console.ReadKey();
            }
        }
    }


    99927.23
    81889.36

    In the above example, we have created a generic list collection with elements in it, and this is called a data source. Next, we used the IEnumerable interface to query which started with from keyword, and we assigned the condition to get the values which are greater than 10000 this called a query expression. Lastly, we executed a query with the help of the foreach loop to print the desired results on the console.

    Advantages of LINQ

    1. LINQ gives an ability to generate queries for any LINQ-compatible data source.

    2. LINQ gives an ability to use it with C# arrays and collections.

    3. LINQ provides IntelliSense, which means writing more accurate queries easily.

    4. LINQ requires less coding while performing operations on data sources.

    5. The same LINQ syntax can be used to query multiple data sources.

    6. LINQ provides easy transformation for the conversion of one data type to another like SQL data to XML data.

    You may also like:

    About the author:
    Subject Matter Expert of C# Programming at Studytonight.
    Tags:C# TutorialC#LINQ
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS