LINQ

Tram Ho

(part 1)

Query Language Integration (LINQ) is a powerful query language introduced with .Net 3.5 & Visual Studio 2008. LINQ can be used with C # or Visual Basic to query different data sources.

The LINQ tutorial will help you learn the LINQ language with topics from beginner to advanced. These tutorials are divided into a series of related topics, so you can start from a topic that has to be understood first, then gradually learn the other features of LINQ sequentially. The LINQ guide is packed with easy-to-understand explanations, practical examples, helpful tips, information notes and points to remember.

These tutorials are designed for beginners and experts who want to learn LINQ step by step.

So what is LINQ?

LINQ (Integrated Language Query) is a unified query syntax in C # and VB.NET to retrieve data from different sources and formats. It is integrated in C # or VB, thus eliminating mismatch between programming languages ​​and databases, as well as providing a unique query interface for different types of data sources.

For example, SQL is a structured query language used to store and retrieve data from a database. In the same way, LINQ is a structured query syntax built into C # and VB.NET to retrieve data from different types of data sources, such as collections, ADO.Net Dataset, XML Docs, web services and MS SQL Server and other databases.

The LINQ query returns the result as an object. It allows you to use an object-oriented approach on the result set and not have to worry about converting different result formats into objects.

The following example shows a simple LINQ query that receives all strings from an array containing ‘a’.

You will not get the result of a LINQ query until you execute it. LINQ queries can be executed in many ways, here we have used foreach loops to execute queries stored in myLinqQuery. The foreach loop executes a query on the data source and receives the result and then iterates through the result set.

Therefore, every LINQ query must query certain types of data sources whether it be array, collections, XML or other databases. After writing a LINQ query, it must be executed to get results.

Why do we have to use LINQ? To understand why we should use LINQ, let’s look at some examples. Let’s say you want to find a list of Teen students (ages 12 to 20) from a range of Student objects

Prior to C # 2.0, we had to use the ‘foreach’ or ‘for’ loop to browse through the collection to find a specific object. For example, we had to write the following code to find all Student objects from a Student array ages 12 to 20 (for ages 13 to 19):

Using the for loop is cumbersome, un maintainable and readable. C # 2.0 version, can be used to handle this type of scenario, as shown below:

So with C # 2.0, you have the advantage of delegate in finding Student with any criteria. You do not have to use the “for” loop to find Student using different criteria. For example, you can use the same delegation function to find a Student whose StudentId is 5 or named Bill, as follows:

The C # team felt that they were still needed to make the code more compact and readable. So they introduced extension methods, lambda expressions, expression trees, anonymous types, and query expressions in C # 3.0. You can use these features of C # 3.0, which are LINQ’s building blocks, to query different types of collections and get the resulting element (s) in a single statement.

The following example shows how you can use a LINQ query with lambda expressions to find specific student (s) from a student’s collection.

As you can see in the above example, we specify different criteria using the LINQ operator and lambda expression in a single statement. Therefore, LINQ makes the code more compact and more readable and it can also be used to query different data sources. For example, if you have a Student table in the database instead of an array of Student objects as above, you can still use the same query to find Student using the Entity Framework.

(continue …)

Share the news now

Source : Viblo