Javatpoint Logo
Javatpoint Logo

What is LINQ?

LINQ is known as Language Integrated Query, and it is presented in .NET 3.5 and Visual Studio 2008. The excellence of LINQ is it gives the capacity to .NET languages(like C#, VB.NET, and so forth) to create inquiries to recover information from the information source. For instance, a program might get data from the understudy records or get to worker records, and so on. In previous years, such sort of information is put away in a different data set from the application, and you really want to learn various kinds of question language to access such kind of information like SQL, XML, and so forth. And furthermore, you can't make a question utilizing C# language or some other .NET language.

To beat such kinds of issues, Microsoft created LINQ. It connects one more capacity to the C# or .NET dialects to create a question for any LINQ viable information source. What's more, the best part is the sentence structure used to make a question is similar regardless of which kind of information source is utilized means the grammar of making an inquiry about information in a social data set is the same as that used to make question information put away in a cluster there is compelling reason need to utilize SQL or some other non-.NET language component. You can likewise utilize LINQ with SQL, with XML records, with ADO.NET, with web administrations, and with some other information base.

In C#, LINQ is available in System. Linq namespace. It gives different sort of classes and techniques which upholds LINQ questions. In this namespace:

  1. The Enumerable class holds a standard query operator that works on an object which executes IEnumerable<T>.
  2. The queryable class holds standard inquiry administrators that work on an object which executes IQueryable<T>.

For Example: SQL is a structured query Language that is used to save and recuperate data from the database. Likewise, LINQ is an Organized Query Sentence structure. LINQ is the basic C#. It is utilized to recover information from various kinds of sources, for example, XML, docs, collections, ADO.Net DataSet, Web Service, MS SQL Server, and different database servers.

What is LINQ

Uses of LINQ

  1. The primary reason behind making LINQ is, before C# 3.0, we used for loop, for each loop, or delegates to traverse a collection to track down a particular object, yet the detriment of involving these strategies for finding an object is you really want to compose an enormous amount of code to find an object which is additional tedious and make your program less lucid. So to defeat these issues, LINQ is presented, which plays out similar activity in a couple of quantities of lines and makes your code clear; furthermore, you can involve similar code in different projects.
  2. It additionally gives full sort checking at the compile time. It assists us with distinguishing the error at the runtime, so we can undoubtedly eliminate them.
  3. LINQ is it is straightforward, very much arranged, and significant level language than SQL
  4. You can likewise utilize LINQ with C# array and collections. It provides you with another guidance to take care of the old issues in a powerful way.
  5. With the assistance of LINQ, you can undoubtedly work with a data source like XML, SQL, Entities, objects, and so forth. A single query can work with the database, compelling reason need to learn various sorts of languages.
  6. LINQ upholds query expression, Anonymous types, Implicitly typed variables, Lambda expressions, Object and collection initializers, and Extension methods.

We can use LINQ queries in two ways

LINQ query Syntax structure is comprised of query keywords which are characterized into the .NET System rendition 3.5 or Higher. This allows the software engineer or developers to compose the directions very much like SQL design in the code(C# or VB.NET) without the utilization of rates. It is in like manner known in light of the fact that the Question Articulation Grammar. In LINQ, you can compose the inquiry to IEnumerable series or IQueryable information sources utilizing the ensuing strategies:

1. Query Syntax:

The LINQ query language syntax starts with from keyword and finishes with the Select or GroupBy keyword. After from keyword, you can utilize various sorts of Standard Query operations like grouping, filtering, and so on, as indicated by your need. In LINQ, 50 unique kinds of Standard Question Administrators are accessible.

Steps for writing the Query syntax:

Step-1: In the first step, we have to add the System.Linq namespace in the code.

Step-2: In a second step, we have to create the data source on which we have to perform the operations

Step-3: In the third step, we have to create the query for the data source using a keyword like select, from, etc.

Here r is the query variable that stores the result of the query articulation. The form clause is utilized to determine the information source, i.e., list, where condition applies to the channel, i.e., l.Contains(" Hii ") and select statement gives the kind of the brought things back. Furthermore, l is the reach variable.

Step-4: The final step is to execute the query using the for each loop.

Example program on Query syntax:

Output:

What is LINQ

2. Method Syntax

In LINQ, Method Syntax is utilized to call the expansion method for the Enumerable or Queryable static classes. It is also called Method Extension Syntax or Fluent. Notwithstanding, the compiler generally changes over the query syntax in method syntax structure at compile time. It can summon the standard Query operator like Where, Join, Max, Min, Avg, GroupBy Select, and so forth. You are permitted to call them straightforwardly without utilizing Query syntax.

Step-1: In the first step, we have to add the System.Linq namespace in the code.

Step-2: In a second step, we have to create the data source on which we have to perform the operations

Step-3: Now create the query using the methods provided by the Enumerable or Queryable static classes

Here r is the query variable that stores the result of the query articulation. The form clause is utilized to determine the information source, i.e., list, where condition applies to the channel, i.e., l.Contains(" Hii ") and select statement gives the kind of the brought things back. Furthermore, l is the reach variable.

Step-4: The final step is to execute the query using the for each loop.

Example program on Method syntax:

Output:

What is LINQ

Advantages of LINQ

  1. The client doesn't have to learn new query languages for an alternate type of data format or data source.
  2. It increments the clarity of the code.
  3. The query can be reused.
  4. It gives type checking of the object at assemble time.
  5. It gives IntelliSense to conventional collections.
  6. It tends to be utilized with collections or arrays.
  7. LINQ upholds ordering, grouping, filtering, and sorting.
  8. It makes debugging simple since it is coordinated with the C# language.
  9. It gives straightforward change suggesting you can without a doubt change more than one data type into another data type like changing SQL data into XML data.

Next TopicNZEC in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA