C# LINQ
LINQ is an acronym for Language Integrated Query. It is Microsoft’s .NET Framework component. Traditionally before the c# LINQ introduction, developers have to learn multiple languages for retrieving and saving data for different data sources. For example, if you want to retrieve data from SQL or Oracle database you must learn some basics of SQL query language, and for retrieving data from XML files you need to learn XML parsers.
LINQ provides a standardized query language to query different data sources. You can retrieve and save data into SQL/Oracle database with the same code. Also, LINQ contains a set of extension methods that allow us to directly query data source objects to filter, group, join results. There are various LINQ enabled data sources that support query through LINQ like a collection of objects, databases or XML files. We can easily query and retrieve data from any object that implements the IEnumerable interface.
Here is the pictorial representation of the LINQ workflow with different variations:
Advantages of LINQ
- LINQ offers an object-based, language-integrated way to query over data irrespective of the source of data. So through LINQ, we can query databases, XML, and collections.
- Compile-time type checking and dynamic query composition.
- LINQ queries are easier to debug.
- It allows you to query on collections like arrays, enumerable classes, etc on any of the language you use in your application like VB or C#. This is much the same way as you would query a database using normal SQL.
- It also supports sorting, filtering, ordering, grouping.
Types of Linq
Various types of LINQ that are available in C# and vb.net are :
- LINQ to Objects: It allows you to query any kind of C# in-memory objects, like arrays, lists, generic lists, and other collection types. LINQ to object query returns an IEnumerable collection.
- LINQ to XML(XLINQ): Using this we can query, modify XML document and also save the document after modification.
- LINQ to DataSet
- LINQ to SQL (DLINQ)
- LINQ to Entities
There are two ways in which we can write queries in LINQ :
- Using Query Syntax
- Using Method Syntax
LINQ Query Syntax in C#
Query syntax consists of a set of clauses written in a declarative syntax and looks very similar to SQL Queries. The .NET Framework CLR does not understand these queries. Hence, they are translated to method syntax at compile time.
Let’s look at a Query syntax example –
int[] Num_arr= { 5,6,7,8,9,0 };
IEnumerable result = from numbers in Num_arr
where numbers >7
select numbers;
Here in the above syntax example, we are simply using an integer array which is having some number elements and we will print the array elements whose value is greater than 7.
So, here in this example, we are printing the selected data from the ‘Num’ integer array based on where condition applied using where clause we. In the output, we will get the elements from Num integer array where numbers value greater than 7 i.e in output we get 8,9.
Every LINQ query operation has three steps :
- Obtain the data source.
- Create the query.
- Execute the query.
The example above shows how the three parts of a query operation are expressed in source code. Here we have used an integer array as a data source. Similar methods apply to other data sources too.
LINQ Method Syntax in C#
In LINQ, Method Syntax uses extension methods included in the Enumerable or Queryable static classes. It is also termed as Method Extension Syntax or Fluent.
However, the compiler converts the query syntax in method syntax at compile time.
Let’s look at a Method syntax example –
List query_list = new List() {
"This is my Dog",
"Name of my Dog is Robin",
"This is my Cat",
"Name of the cat is Mewmew"
};
var result_list = query_list.Where(s => s.Contains(“Dog”));
Here result_list is the query variable which stores the result of the query expression. In the output list, we are getting those strings that do not contain “Dog” in it. We have used Where() method to filter the data source according to the lambda expression given in the Where() method. (s => s.Contains(“Dog”)).
After executing the query using for each loop we will get following in the output:
This is my Dog
Name of my Dog is Robin
Knowledge of .NET is quite rewarding in the IT industry. If you have got some skills in the .NET framework then a .NET certification from StudySection can prove to be a good attachment with your resume. You can go for a foundation level certificate as well as an advanced level certificate in the .NET framework.