In this post I introduce you to Linq and some of its very important features.

INTRODUCTION

In my last white paper I had touch based functional programming. Linq is one of the pillars to the functional programming. LINQ ---> Language INtegrated Query is the new technology from Microsoft. It has now become core part of the .NET Framework 3.5 Up till now developers were querying the tables but now with LINQ they can query the tables as well as objects. By objects I mean to say the entities or classes which implements IEnumerable interface. E.g. Arrays, Collections, Lists …. and others Each data model has its own definition of querying, e.g. For Tables we use SQL, for XML we have Xquery or DOM. Linq is very generic in nature and same fundamentals of its can be applied in any form of the data. I know some would question what about ODBC, but my friends we are leaving in modern age where the applications have become more complex and ODBC is out of window. Linq is the technology that unifies the implementation of access of any type data. It introduces the programming model as a first-class concept into any .NET language. These extensions boost up developer productivity, thus reducing the shorter and meaningful code. All Linq objects are type safe. As all the data is strongly typed Visual Studio provides intelligence for all Linq programming. Language integration is a fundamental aspect of Linq. The most visible part is the query expression feature present in the C# 3.0 and VB 9.0 This technology will have a great impact on how the software will be written. It would be misconception that it will change the architecture of the application, because its goal is to provide set of tools that improve the code implementation by adopting different architectures. Some developers may ask what will happen to T-SQL or PL-SQL now with the invent of LINQ. This legacy technology will remain but whatever you can do in these Procedural languages the same thing can be achieved through LINQ but even more and that too keeping the modern standards. To console you further the syntax of SQL and LINQ are very much same. The same operators will work (Where, From etc) Last but not least Parallel Linq (PLINQ) is a research project on parallel or multi cores computing that can be obtained by writing code with Linq

LINQ FLAVOURS

Linq comes in various flavors

Each of these implementations is defined through the set of extension methods which implements the operator that is needed by Linq to run over particular data domain. Linq to Objects This implementation can be used to manipulate collections of objects which are related to each other in some hierarchy E.g.

Public Sub Linq1()

'This query will give us all numbers in the array which are less than 5

'The variable num is called the "range variable" - it will take turns 'representing each value in the array. 'Creates an array of numbers

Dim numbers = New Integer() {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}

Dim lowNums = From num In numbers _ Where num < 5 _

Select num

Console.WriteLine("Numbers < 5:") 'lowNums now contains only the numbers that are less than 5

'We can use a loop to print out each of the values

For Each lowNumber In lowNums

Console.WriteLine(lowNumber)

Next

End Sub

Linq to ADO.NET• Linq to SQL: This implementation handles the mapping between custom types of programming language and physical table in database. E.g.

Private db As NorthwindDataContext

Public Sub LinqToSQL1()

'Instead of returning the entire Customers table, just return the 'CompanyName and Country

• Linq to Entities: It is more or less similar to above implementation but instead of physical database it uses the conceptual Entity Data Model (EDM). Yes you guess right I am talking about ADO.Net Entity Framework. This framework comes as part of .NET Framework 3.5 Service Pack 1. It produces as abstract data layer independent of physical database layer.

• Linq to Datasets: This implementation helps to query a dataset. E.g.

Public Sub DataSetLinq()

Dim products = TestDS.Tables("Products")

Dim productNames = From prod In products _

Select prod!ProductName

Console.WriteLine("Product Names:")

For Each productName In productNames

Console.WriteLine(productName)

Next

End Sub

Linq to XML This implementation allows querying the Xml data. VB 9.0 has upper hand here than its counter part C# because VB 9.0 can include xml literals in the language. E.g. Public Sub XMLLinq()

Dim doc = XDocument.Load(dataPath & "nw_customers.xml")

Dim custQuery = doc..(0).Elements()

For Each result In custQuery

Console.WriteLine(result)

Next

End Sub

CLOSURE

Linq is the best data abstraction and unified platform to query various data sources. I strongly recommend the developers to pull up their socks and get started with this technology. As this is new mantra of querying any data source. The declarative syntax of Linq makes it interesting choice also over the conventional data access. I already told you about PLinq. Till then Good Bye… Happy Linquing

About the Author

http://goldytech.wordpress.com
Hello Everyone
Myself Muhammad Afzal , aka GoldyTech. Thats my pen name.
I reside in India and work as a solution Architect on .NET platform.
I hope you must have enjoyed reading my blog. Please leave your comments or suggestions good or bad and help me to improve