Thursday, March 13, 2008

Simple LINQ Demo

Everyone here now must be aware of the revolutionary model of Data Connectivity and Data Handling. I just saw few days ago some articles posted on Internet but for newbie like me who are just came out from college in such big Developer community. Its very difficult to judge these days what to learn, when to learn and from whom to learn..isn't it..? ;-)

I was shocked firstly when I saw few Videos and Articles on LINQ , I done lots of C programming in My MCM Course for more than a year, but I never query any collection or string. I think future is bright for Data Connectivity, Right now I everyday have debate in my company over the MVC (Model View Controller) pattern of Data connectivity, Some says its quite slow because of many internal trips in Classes, Some says its the only best, but what I observe is Web Service is superfast!!

Humm..And one last thing..Don't try this on Visual Studio 05 ;-) .. This VS08 is awesome!!

Where here my code goes which is basically a C# console application, I can say that these code snippets can fall under category of LINQ to Objects, I am using IEnum

Make sure before you start coding that you have following namespace included

System.Linq and System.Linq.Expressions

static void Main(string[] args)

{

string[] friends = { "Vikram", "Chaitanya", "Ameya", "Sid" };

//Normal declaration of string which contains few names for example

//Below is Query which we are executing over our pre-defined collection

IEnumerable<string> firstname = from v in friends

where v.Contains("i") && v.Length <7

orderby v ascending

select v;

//Below is plain simple loop which will just print the query result

foreach (string name in firstname )

{

    Console.WriteLine(name);

}

//Still I am not forgetting getch() in C

Console.ReadLine();

}

Some of the query combination you can try on with && and || operator

  1. Using "And" operator i.e &&

    IEnumerable<string> firstname = from v in friends

where v.Contains("i") && v.Length <7

orderby v ascending

select v;

  1. Using "Or" operator i.e ||

    IEnumerable<string> firstname = from v in friends

where v.Contains("i") || v.Length <7

orderby v ascending

select v;

Well..I hope this will give some idea how LINQ looks like..will do now LINQ to XML soon.

Vikram.

2 comments:

InnerWorkings said...

Agree that it's hard to know what to learn and where to start...check out this free LINQ and VS 2008 offer from InnerWorkings.

3 hours of free hands-on training on .NET 3.5 and Visual Studio 2008:

* New Features in C# 3.0
* New Features in VB 9.0
* LINQ to SQL

You can check it out here: http://www.innerworkings.com/promotions/75d4bd51-4bf1-4d8a-8c47-73135e44837a/visual-studio-2008-promotion

Cheers,

Brian Finnerty
http://blogs.innerworkings.com/brian-finnerty

Vikram Pendse said...

Thanks Brian ! :)