New features of C Sharp 3.0 LINQ (Language Integrated Query)
Posted on 5/13/2007 8:54:59 AM
in #C Sharp
Hi/P>
LINQ (Language Integrated Query) is the composition of many standard query operators that allow us to work with data of any datasource in a very intuitive way. LINQ provide compile time checking of query and the ability to debug through query./P>
To show a very basic example of what can be done with the help of LINQ I am using and in memory generic list collection that queried by LINQ./P> private static List<string> VikramObj = new List<string>() /o:p>/SPAN>/PRE>{ "www.vikramlakhotia.com", "www.justlikethat.vikramlakhotia.com", "Vikram", "Lakhotia" };/o:p>/SPAN>/PRE>public static void VikExample () /o:p>/SPAN>/PRE>{/o:p>/SPAN>/PRE> /SPAN>IEnumerable<string> query = from v in VikramObj select v;/o:p>/SPAN>/PRE> /SPAN>foreach (string VikramObj in query) /o:p>/SPAN>/PRE> /SPAN>{ /SPAN>Console.WriteLine(VikramObj); /SPAN>}/o:p>/SPAN>/PRE>}/o:p>/SPAN>/PRE>
I know this can be done very easily with the help of a foreach loop on the collection. But the example is only for basic understanding on LINQ. To make thing just a little more complicated we can also use where clause in the query. Here is an example where I am checking the length of the string is also greater than 15 and then sorting the records. /P>
public static void VikExample1() /P>
{/P>
/SPAN>IEnumerable<string> query = from v in VikramObj where v.Length > 15 /P>
/SPAN>orderby v select v;/P>
/SPAN>foreach (string VikramObj in query) /P>
/SPAN>{ /SPAN>Console.WriteLine(VikramObj); /SPAN>}/P>
}/P>
Thanks Vikram/P>
|