What is LINQ?

LINQ is an acronym for the Language Integrated Query which was released in year 2007 as a part of .NET framework 3.5. It makes possible to query data in .NET supported languages. It defines a set of Standard Query Operators that will make it possible to get the data on several sources. There is an API named as Standard Query Operator API, which defines the query operators. Select, where and Select Many are some of the frequently used ones among them. SQL syntax provides a short hand method for invocation.


Advantages of Using LINQ

There are four main advantages of LINQ. They are,

The availability of strong typed queries: The classes are auto generated according to the relations in relational databases. The language is very much easy to understand as in SQL.

The automatic join functioning in foreign keys: In normal SQL, the user has to join the tables if it is necessary. In LINQ, it provides the ability to join each function automatically when there is a foreign key reference.

The code size: There are many occasions that the users have to write long sentences for getting a SQL query. LINQ provides relatively short codes in such advanced occasions. It reduces the complexity of the code and makes it much easy for the program to read.

Code equality: One of the most advantages in using LINQ is that its availability over any .NET platform language such as C#.net, VB.NET and F#.NET.

LINQ to Objects and XML

The local query execution engine in LINQ can be used for the purpose of querying memory in collections. It allows IEnumerable collections to be queried locally while defining it in the sequence pattern. The currently available LINQ to Objects does not allow complex queries, but the simple and linear queries. The XML document is converted to the XElement objects through the LINQ to XML provider. The XElement objects are queried then against the local search engine.


Both SQL Server Databases and SQL Server Compact Databases can use the LINQ to SQL provider. When the database is available in a remotely located SQL server, LINQ converts its queries to the SQL queries and then it is sent to the SQL server for the processing. LINQ is used as encapsulated data objects as the SQL server stores data as relational data. Those two should map each other with the support of LINQ to SQL mapping framework. There the classes being made according to the name of the database table. All or a part of the columns of the table are being taken as the data members. SQL defined attributes are used for the purpose of relating the other relational model attributes.

What LINQ Can Do More Than SQL?

Structured Query Language is designed for the purposes of querying data, updating, deleting or inserting from a database. What LINQ can do when compared to the SQL is very much. The users can call to several data sources not only a database, by using LINQ. It can take a single enumeration by calling to a database as well as a file system. Most important advantage of LINQ is the reduction of the code length. When executing a large query the length of SQL code is very much larger when comparing to the Method Based Query in LINQ.

LINQ to DataSet

The DataSets must be populated before using them. The way how data is loaded is not that important in this case. Two methods can be used for the purpose of loading data. The data can be retrieved directly from a database or the users can take the advantage of DLinq to query the database and then load the data that comes as the result into a DataSet. Either, you can use DataAdapter to querry data from the databases. Querying data is possible after loading them. There is an enumerate querried through the LINQ. The different between a normal enumeration and an enumeration that get from a LINQ query is that the LINQ querry gets an Enumeration of a DataRow object rather than getting a data set of a custom type. The DataSets can be devided into two main parts as typed and untyped. Typed DataSet declares the corresponding data type such as bool, int, date or etc.

TableAdapters are also can be used in case of querying. The TableAdapter is an adapter that is configured early and encapsulates the connection information, and the queries also predefined and exposed as methods that can be used for filling an already created table or creating a new table and working on it to fill.

There are number of custom operations can be made for the purpose of enabling LINQ queries over the DataSet.

Security Matters in LINQ

There are several matters both positive and negative that users have to concern when using LINQ. The security concerns vary from the reason and the place where the code is used.


SQL injection attacks have been a very much popular threat than SQL always had with it. One of the recommended techniques that is said to use is that writing the code using parameters. LINQ is always used as parameter manner. That threat is reduced for certain extend.


Extensible Markup Language is used for the purpose of transporting data. So the security is very much concerned in this matter. According to the Microsoft releases it is not yet advised to use LINQ in places where security is concerned a lot. This application is not much for the server-side applications other than the programming convenience. If it is very much need to use LINQ to XML for such advanced scenarios, the only acceptable way is creating an instance of an xmlReader class that can filter out the XML DOS attacks.

When Not to Use LINQ

LINQ is a good implementation that could help lot of areas in a very convenient manner. In most of the LINQ make things much easy than the others. But using LINQ for small SQL queries might not be suitable at all. In addition when working with LINQ to XML, one of the major concerns should be avoiding the Denial of Service attacks.


Tags: #Microsoft #SQL