An introduction to LINQ
By
Sanket Chaukiyal |
Updated on 05-Sep-2011
To put it very simply, Language-Integrated Query or LINQ is another way to query data using native code. It adds native querying capability to .NET languages. You may ask “what is the need of another data access method when you already have a good one like ADO.Net?” Well, one of its major advantages is that, it is a general-purpose query language, which essentially means that it is not just used to query relational databases but also XML, Controls and Memory Data Objects. Curious? Let me show you how. Consider the following examples:
SQL Statement in ADO.NET
Select * from Inventory where Price >50;
LINQ statement
var product=from d in Inventory
where d.Price >50
select d.ProductName;
Simple enough right? But you’d wonder what’s the advantage in using LINQ instead of an SQL statement in ADO.NET. Consider this, in ADO.NET you will pass the SQL statement as a string command but in LINQ you are retrieving data using native code which has the benefit of intellisense. This ensures that there are no errata and all the other issues related with passing raw SQL commands as strings are resolved.
By using var we let the compiler “guess” the type of variable we want. Now, if you say that we could have directly used “string” in the above example then you are right. But take a scenario where we want to recover all the data for the given product which contains integers as well as string, then we have no option but to use “var” with which we retrieve all types of data.
Not convinced yet? There’s more.
Window Form controls
Imagine that you have a data entry form where all the text fields are supposed to be filled. Here is a simple way to test if the text fields are filled.
var FormTextBoxes = from MyControl in this.Controls.OfType<TextBox>()
where MyControl is TextBox
select MyControl;
foreach(TextBox textbox in FormTextBoxes)
{
if (textbox.Text==String.Empty)
{
textbox.Text = “Please enter data”;
textbox.BackColor = Color.Aqua;
}
}
Well, that was a simplistic code but you get the drift of what is possible and how it can drastically shorten your code.
Memory Data Object:
int[] age = { 45, 23, 77, 87, 12, 09, 86 };
IEnumerable<int> query = from i in age
where i > 77
select i;
foreach (int item in query)
{
Console.WriteLine(item);
}
LINQ makes access to data memory objects as easy as the snap of your fingers. It treats the memory data object as just another data source and accesses it just as easily as it accessed an SQL data source or a collection of controls in a Windows form.
XML Data Source
LINQ also simplifies access to an XML data source. Consider the following XML document MyContacts.xml:
<contacts>
<contact>
<firstName>Prakash</firstName>
<lastName>Govindasreenivasan</lastName>
<city>Pune</city>
</contact>
<contact>
<firstName>Roy</firstName>
<lastName>Joseph</lastName>
<city>Hyderabad</city>
</contact>
</contacts>
Now see the LINQ code used to access the XML data. Plain as vanilla.
XDocument ContactDocument = XDocument.Load(@”C:MyContacts.xml”);
var q = from c in ContactDocument.Descendants(“contact”)
where c.Element(“city”).Value == “Hyderabad”
select c.Element(“firstName”).Value;
foreach (string name in q)
{
Console.WriteLine(item);
}
Wasn’t that really simple? RSS feed access would be an interesting and useful application. The data source will change from an XML document to an RSS feed (which is XML as well) but the code for traversing through XML data will remain the same.
LINQ is really great for rapid application development and can be learned relatively quickly; although,, by no means is it a complete replacement for other data access methods. Complex operations such as locking, serialization and transaction management are best performed in SQL.
LINQ really makes it simple to query just about anything that holds data in .NET, and it does so using the same syntax with a little variation for different data sources. Although the learning curve for LINQ is quite simple considering all the examples you saw above, it can perform more powerful functions as well.