Select in LINQ

Select in LINQ

Today we will talk about how to work with LINQ. We will start with how to select data using the LINQ query. Select in LINQ means selecting data using the LINQ query. If you are aware that how to select data in SQL then it will be easy for you to understand the LINQ query. Don’t worry if you do not know SQL because I will be writing the LINQ query and explaining it to you.

Don’t worry about this word query, the query word, you can think of a question mark sentence in English. It is like you are asking a question to DBMS.

What is DBMS?

The DBMS (Database Management System) is software that handles the database or databases. You can say the DBMS is a security guard for the databases that will not allow changes or anyone to see the data until the request is coming from an authorized user.

Every time you want to perform some operations to the database, you tell the DBMS in the form of a query and DBMS performs the operations on the database as per the query logic. If the query logic is making sense to the DBMS then you will get the action performed, otherwise, you will get an error from DBMS.

Now that you have understood what is a query and what is DBMS, let us come back to our LINQ Select query. Just like the English language when you write or speak a sentence you follow some rules, you follow some rules while writing LINQ queries.

There are four types of queries.

  1. Create
  2. Read
  3. Update
  4. Delete

Today we are discussing select query which is a read type of query, where we are reading some data from the database.

Lets take an example of Select in LINQ

using System;
using System.Linq;

namespace Namespace
{
    class Program
    {
        
        static void Main(string[] args)
        {
            int[] age = {12,22,32,43,13,54,23,40,42,66,34,32,77,19,21,25,29,76,80,33,39,64};

            var a = from i in age where i > 20 select i;

            foreach (var i in a)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();



        }
    }
}

Program Output

Select-in-LINQ-program-output

Program Explanation

The line from i in age where i > 20 select i; is a LINQ query to select the data from an Array. Be informed of the above example of selecting data from an Array but LINQ can select data from all types of resources as we have already understood this in our last What is LINQ article.

If you are thinking about why the query is starting with “From”. The LINQ has its own syntax that you will have to learn just like English has its own rules of writing a sentence.

from – The query starts with the “from” keyword, you can consider this as a filler word to keep it simple.

i – “i” is a temporary variable where we will be storing all the elements of the age array variable.

in – The “in” keyword is used to tell the DBMS where it should look for finding the desired data. In the above program, the desired data is in the age variable.

where – The keyword where is used to tell the DBMS there is a condition when looking for the desired data and that condition comes after the where keyword. In the above program, the condition is “i > 20”. We already know i is a temporary variable that is storing the elements of our age array. It means the condition is “age > 20“.

select – This keyword is used to tell the DBMS, the data that is stored inside the “i” variable based on the condition which is “i > 20” should be selected.

Further in the program, we have used an implicit type variable and passed it the data saved inside the temporary variable “i”. We saved this data to a permanent variable “a” because “i” is a temporary variable. Do not worry about what is an implicit type of variable. That is a topic for another article. At this point, you can consider it a permanent variable.

A further foreach loop is used and we read all the elements of the permanent variable and printed it on the console. I know you might be confused about foreach. Just know that it is a type of loop that is used to reach the elements of an array.

You can see in the output screenshot, the selected elements are as per the condition. The condition is the element should be smaller than 20 so out of 22 elements of the array only 19 are selected. The 3 elements 12, 13, and 19 are left out.

I hope, I was able to make you understand the query. In case of any confusion, feel free to comment. I would be glad to help.

Take Care guys!