New features of C Sharp 3.0 LINQ (Language Integrated Query)

Hi

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.

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.

private static List<string> VikramObj = new List<string>() 
{ "www.vikramlakhotia.com", "www.justlikethat.vikramlakhotia.com", "Vikram", "Lakhotia" };
public static void VikExample () 
{
  IEnumerable<string> query = from v in VikramObj select v;
  foreach (string VikramObj in query) 
  {   Console.WriteLine(VikramObj);  }
}

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.

public static void VikExample1()

{

  IEnumerable<string> query = from v in VikramObj where v.Length > 15

  orderby v select v;

  foreach (string VikramObj in query)

  {    Console.WriteLine(VikramObj);  }

}

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia