Google Search

Google
 

Thursday, April 3, 2008

LINQ Introduction

Language Integrated Query (LINQ), a new query language feature in .net 3.5.

You can use it to retrieve data from an array or collection or any data source that supports IEnumerable or IQueryable.

Let's see example how to use LINQ in string array.We have string array of country like this,


Dim countries As String() = {"Australia", "Brazil", "China", "Japan", "India", "United States", "United Kingdom"}
Dim data = From country In countries Where country.StartsWith("Uni") Select country.ToUpper
Response.Write("Country List</br">")
For Each country As String In data
Response.Write(country & "</br">")
Next


Here, you have only change "LINQ Query" with query that discuss below.

==> By using LINQ you can retrive data from array. Below LINQ equivalent to SELECT * statement of SQL Server.

From country In countries Select country

==> You can also use conditional statement in array to get data.To retrive country list that have string length less than 6.

From country In countries Where country.Length < 6 Select country

==> You can also sort an array Ascending/Descending.

From country In countries Order By country Descending Select country

==> To retrive Country name that start with "UNI" and country name in Upper case.

From country In countries Where country.StartsWith("Uni") Select country.ToUpper

Here, I have explain how use LINQ.