LINQ
Language-Integrated Query
Last updated
Language-Integrated Query
Last updated
The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).
LINQ is a uniform structured query syntax built in C# and VB.NET to retrieve data from different types of data sources and formats such as collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server, and other databases.
LINQ 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.
Familiar language: Developers don’t have to learn a new query language for each type of data source or data format.
Less coding: It reduces the amount of code to be written as compared with a more traditional approach.
Readable code: LINQ makes the code more readable so other developers can easily understand and maintain it.
Standardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources.
Compile time safety of queries: It provides type checking of objects at compile time.
IntelliSense Support: LINQ provides IntelliSense for generic collections.
Shaping data: You can retrieve data in different shapes.
LINQ queries uses extension methods for classes that implement IEnumerable
or IQueryable
interface. The Enumerable
and Queryable
are two static classes that contain extension methods to write LINQ queries.
Points to Remember:
Use System.Linq
namespace to use LINQ.
LINQ api includes two main static class Enumerable & Queryable.
The static Enumerable class includes extension methods for classes that implements the IEnumerable<T> interface.
IEnumerable<T> type of collections are in-memory collection like List, Dictionary, SortedList, Queue, HashSet, LinkedList.
The static Queryable class includes extension methods for classes that implements the IQueryable<T> interface.
Remote query provider implements e.g. Linq-to-SQL, LINQ-to-Amazon etc.
Q. Explain how LINQ is useful than Stored Procedures?
Debugging: It is difficult to debug a stored procedure but as LINQ is part of.NET, visual studios debugger can be used to debug the queries
Deployment: For stored procedure, additional script should be provided but with LINQ everything gets compiled into single DLL hence deployment becomes easy
Type Safety: LINQ is type safe, so queries errors are type checked at compile time
System.Data.Dlinq.dll provides the functionality to work with LINQ to SQL
Q. Explain what is lambda expressions in LINQ? Lambda expression is referred to as a unique function use to form delegates or expression tree types, where right side is the output and left side is the input to the method. For writing LINQ queries particularly, Lambda expression is used.
Q. Mention what is the role of DataContext classes in LINQ? DataContext class acts as a bridge between SQL Server database and the LINQ to SQL. For accessing the database and also for changing the data in the database, it contains connections string and the functions.