LINQ is known as Language Integrated Query and it is introduced in .NET 3.5 and Visual Studio 2008. The beauty of LINQ is it provides the ability to .NET languages(like C#, VB.NET, etc.) to generate queries to retrieve data from the data source. For example, a program may get information from the student records or accessing employee records, etc. In, past years, such type of data is stored in a separate database from the application, and you need to learn different types of query language to access such type of data like SQL, XML, etc. And also you cannot create a query using C# language or any other .NET language.
To overcome such type of problems Microsoft developed LINQ. It attaches one, more power to the C# or .NET languages to generate a query for any LINQ compatible data source. And the best part is the syntax used to create a query is the same no matter which type of data source is used means the syntax of creating a query data in a relational database is same as that used to create query data stored in an array there is no need to use SQL or any other non-.NET language mechanism. You can also use LINQ with SQL, with XML files, with ADO.NET, with web services, and with any other database.
In C#, LINQ is present in System.Linq namespace. It provides different type of classes and methods which supports LINQ queries. In this namespace:
The architecture of LINQ is 3-layered architecture. In which the topmost layer contains language extension and the bottom layer contains data sources that generally object implementing IEnumerable <T> or IQueryable <T> generic interfaces. The architecture of the LINQ is as shown in the below image:
Now we learn why LINQ is created, or why we use LINQ. The following points explain why we use LINQ.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Language-Integrated Query (LINQ) is a powerful set of technologies based on the integration of query capabilities directly into the C# language. LINQ Queries are the first-class language construct in C# .NET, just like classes, methods, events. The LINQ provides a consistent query experience to query objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).
LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.
For example, SQL is a Structured Query Language used to save and retrieve data from a database. In the same way, LINQ is a structured query syntax built in C# and VB.NET to retrieve data from different types of data sources such as collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server and other databases.
LINQ UsageLINQ queries return results as objects. It enables you to uses object-oriented approach on the result set and not to worry about transforming different formats of results into objects.
The following example demonstrates a simple LINQ query that gets all strings from an array which contains 'a'.
Example: LINQ Query to Array
// Data source
string
[] names = {"Bill", "Steve", "James", "Mohan" };
// LINQ Query
Explore more:
Are Microwave Paper Bags Safe for Reheating Food?
Where do you get postcards?
What Is Sublimation Paper? The Ultimate Guidevar
myLinqQuery = from
name in
names
where
name.Contains('a')
select
name;
// Query execution
foreach
(var
name in
myLinqQuery)
Console
.Write(name + " ");
In the above example, string array names is a data source. The following is a LINQ query which is assigned to a variable myLinqQuery
.
from
name in
names
where
name.Contains('a')
select
name;
The above query uses query syntax of LINQ. You will learn more about it in the Query Syntax chapter.
You will not get the result of a LINQ query until you execute it. LINQ query can be execute in multiple ways, here we used foreach
loop to execute our query stored in myLinqQuery
.
The foreach
loop executes the query on the data source and get the result and then iterates over the result set.
Thus, every LINQ query must query to some kind of data sources whether it can be array, collections, XML or other databases. After writing LINQ query, it must be executed to get the result.
Learn why should we use LINQ in the next chapter.
Previous: Choosing the Right Graphic Printing Film
Next: C# Any Method
Comments
Please Join Us to post.
0