How to Use LINQ (Language Integrated Query) C#

LINQ (Language Integrated Query)

The queries written in SQL are not understandable by C# compiler. If there are errors in an SQL query, then the C# compiler is not able to detect it, and it simply compiles the SQL query irrespective of whether it is correct or incorrect. For resolving this problem, LINQ was developed for C#. Now LINQ is integrated with C# compiler. So, when the programmer or user makes a mistake in a query the C# compiler detects that mistake and the program is not compiled until the mistake in the query is not corrected. LINQ can be used to query databases, XML files, and Class objects.

Types of LINQ queries:

There are three types of LINQ queries in C#:

  1. LINQ to Object
  2. LINQ to DataSet
  3. LINQ to XML

Language Integrated Query to Object:

This type of query is used to query Collections in C# such as Dictionary, ArrayList, Queue, and Stack. To explain this type of query let us consider the following example:

Now in this example, we are demonstrating Array List in which we are applying LINQ.


LINQ Employee Example:


Language Integrated Query to DataSet:

Language Integrated Query to DataSet is a format of writing the query for datasets. In other words, as we can access data within tables with the help of SQL. We can do the same thing in C# with the help of LINQ. LINQ helps us to search for data in databases, replace data and also remove and insert data in databases. To explain this let us consider the following example:


Leave a Reply

Your email address will not be published. Required fields are marked *