Saqib Ullah

BootStrapper Know How

  Home  |   Contact  |   Syndication    |   Login
  93 Posts | 1 Stories | 336 Comments | 15 Trackbacks

News



Article Categories

Archives

Post Categories

Blogging websites

Favourite Blogs

Private Links

Sites

Microsoft Babies
Microsoft new baby yes I am talking about LINQ stand for Language-Integrated Query is now available as a integral part of Visual Studio Orcas. Microsoft releases the new Visual Studio with the name of Orcas and all Microsoft previous efforts (Windows Communication Foundation WCF, Windows Workflow Foundation WWF, Windows Presentation Foundation WPF, Windows CardSpace and LINQ) are integrated in this Studio. From last one and half years Anders Hejlsberg team done a tremendous job in the overcoming the gap between the data impedance. Mr. Anders team gives a native syntax to developers in the form LINQ to C# and VB.Net for accessing data from any repository. The repository could be in memory object, database (still MS SQL Server only) and XML files.
 
What is LINQ?
Still lots of folks don’t understand what LINQ is doing. Basically LINQ address the current database development model in the context of Object Oriented Programming Model. If some one wants to develop database application on .Net platform the very simple approach he uses ADO.Net. ADO.Net is serving as middle ware in application and provides complete object oriented wrapper around the database SQL. Developing application in C# and VB.Net so developer must have good knowledge of object oriented concept as well as SQL, so it means developer must be familiar with both technologies to develop an application. If here I can say SQL statements are become part of the C# and VB.Net code so it’s not mistaken in form of LINQ. According to Anders Hejlsberg the chief architect of C#.
 
             “Microsoft original motivation behind LINQ was to address the impedance mismatch between programming languages and database.”
 
LINQ has a great power of querying on any source of data, data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable<T> interface. Microsoft basically divides LINQ into three areas and that are give below.
 
  • LINQ to Object {Queries performed against the in-memory data}
  • LINQ to ADO.Net
    • LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
    • LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
    • LINQ to Entities {Microsoft ORM solution}
  • LINQ to XML (formerly XLinq) { Queries performed against the XML source}
 
I hope few above lines increase your concrete knowledge about Microsoft LINQ and now we write some code snippet of LINQ.
 
1. Code Snippet

int[] nums = new int[] {0,1,2};
var res = from a in nums
             where a < 3
             orderby a
             select a;
foreach(int i in res)
    Console.WriteLine(i);
Output:
0
1
2
 
 
All SQL Operates are available in LINQ to Object like Sum Operator in the following code.
 
2. Code Snippet

int[] nums = new int[] {2,4,6,2};
int result = nums.Sum();
Console.WriteLine(result);

Output:
1
2

One thing that I want to share with you guys is LINQ to Object support querying against any object that inherits from IEnumerable (all .Net collection inherits from IEnumerable interface). LINQ to Object provided main types of Operator Type that are give below.
 
 

Operator Types
Operator Name
Aggregation
  • Aggregate
  • Average
  • Count
  • LongCount,
  • Max,
  • Min,
  • Sum
Conversion
  • Cast,
  • OfType,
  • ToArray,
  • ToDictionary,
  • ToList,
  • ToLookup,
  • ToSequence
Element
  • DefaultIfEmpty,
  • ElementAt,
  • ElementAtOrDefault,
  • First,
  • FirstOrDefault,
  • Last,
  • LastOrDefault,
  • Single,
  • SingleOrDefault
Equality
  • EqualAll
Generation
  • Empty,
  • Range,
  • Repeat
Grouping
  • GroupBy
Joining
  • GroupJoin,
  • Join
Ordering
  • OrderBy,
  • ThenBy,
  • OrderByDescending,
  • ThenByDescending,
  • Reverse
Partitioning
  • Skip,
  • SkipWhile,
  •  Take,
  •  TakeWhile
Quantifiers
  • All,
  • Any,
  • Contains
Restriction
  • Where
Selection
  • Select,
  • SelectMany
Set
  • Concat,
  • Distinct,
  • Except,
  • Intersect,
  • Union
 
To the good use of above operator types I need samle patient class so here it

using System;
 
public class Patient
{
    // Fields
    private string _name;
    private int _age;
    private string _gender;
    private string _area;
    // Properties
    public string PatientName
    {
        get { return _name; }
        set { _name = value; }
    }
 
    public string Area
    {
        get { return _area; }
        set { _area = value; }
    }
    public String Gender
    {
        get { return _gender; }
        set { _gender = value; }
    }
    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
}
 
Here is my code that intiliaze patients object with following data.
 

List<Patient> patients = new List<Patient> {
           new Patient { PatientName="Ali Khan", Age=20, Gender="Male" , Area = "Gulshan"},
           new Patient { PatientName="Ahmed Siddiqui", Age=25 ,Gender="Male", Area = "NorthKarachi" },
           new Patient { PatientName="Nida Ali", Age=20, Gender="Female", Area = "NorthNazimabad"},
           new Patient { PatientName="Sana Khan", Age=18, Gender="Female", Area = "NorthNazimabad"},
           new Patient { PatientName="Shahbaz Khan", Age=19, Gender="Male", Area = "Gulshan"},
           new Patient { PatientName="Noman Altaf", Age=19, Gender="Male", Area = "Gulshan"},
           new Patient { PatientName="Uzma Shah", Age=23, Gender="Female", Area = "NorthKarachi"}};
 
Patient p = new Patient();
        p.Age =33; p.Gender = "male";
        p.PatientName = "Hammad Ali"
        p.Area = "Defence";         
        patients.Add(p);
 
I have been written a blog on the new way of initilaztion.

This code snippet fetch those records whose gender is equal to “Male”.
 

   gdView.DataSource = from pa in patients
                        where pa.Gender == "Male"
                        orderby pa.PatientName, pa.Gender, pa.Age
                        select pa;
   gdView.DataBind();
 
The following code snippet uses the selection operator type, which brings all those records whose age is more than 20 years.

 var mypatient = from pa in patients
                  where pa.Age > 20
                  orderby pa.PatientName, pa.Gender, pa.Age
                  select pa;
       
        foreach(var pp in mypatient)
        {
        Debug.WriteLine(pp.PatientName + " "+ pp.Age + " " +          pp.Gender);
        }
 The following code snippet uses the grouping operator type that group patient data on the bases area. 

var op = from pa in patients
group pa by pa.Area into g
select new {area = g.Key, count = g.Count(), allpatient = g};
 
 foreach(var g in op)
 {
    Debug.WriteLine(g.count+ "," + g.area);
    foreach(var l in g.allpatient)
     {
       Debug.WriteLine("\t"+l.PatientName);
     }
 }
This code snippet determine the count of those records, which lay in above 20 years. 

int patientCount = (from pa in patients
                    where pa.Age > 20
                    orderby pa.PatientName, pa.Gender, pa.Age
                    select pa).Count();

All the above codes are few example of LINQ to Object technique of LINQ. In my up coming post you will see both LINQ to SQL and LINQ to XML code snippets.
 
posted on Monday, April 30, 2007 9:51 AM

Feedback

# re: What is LINQ? 8/24/2007 8:44 AM Rakesh Nandrajog
This is really nice topic to gives a hats off to LINQ

# Nice article 9/8/2007 4:02 PM Yohan Liyanage
Very Good article which gives a good starting introduction into Microsoft LINQ.

Thanks Saqib!

# re: What is LINQ? 9/10/2007 1:39 AM Sandip Maniar
Nice article to get an overview about MS LINQ.

Thanks

# re: What is LINQ? 10/31/2007 5:24 PM Charlie
I think LINQ is a little strange

# re: What is LINQ? 12/2/2007 8:56 PM ARTI
I didnot know anything about LINQ.

Now on reading this article , i understand what is linq.
Thank you.

# re: What is LINQ? 12/12/2007 10:07 PM Gopz
PLS GIVE ME A LINK WHICH EXPLAINS CLEARLY ABOUR LINQ

# re: What is LINQ? 12/18/2007 1:53 AM Matt
How in the heck does the sum() in snippet #2 output 1,2 ??

# re: What is LINQ? 12/19/2007 7:19 PM Diwakar Gautam
Reallly Nice Article.

Hope for Some Others

Thanks.

Diwakar

# re: What is LINQ? 12/26/2007 1:45 PM subhash nayak
The above description about LINQ that deals with in-memory objects. How do we use LINQ to access databse??
Thanks and regds
Subhash

# re: What is LINQ? 12/26/2007 4:42 PM Roshan Perera
A very good introduction to LINQ

Thanks

Roshan

# re: What is LINQ? 12/30/2007 6:27 PM suyog.kale
very good article on Microsoft new baby :: LINQ,
really helping me , must be .net deveopers also
Thanks & Rgds,
************************************************************
Suyog Kale
Software Programmer
SpadeWorx Software Services
302, Sai APEX, Dutta Mandir Chowk, Viman Nagar, Pune - 411 014, India

DID : +91-204-010-0506
Mobile : +91-98600-22-945
email : suyog.kale@spadeworx.com
web : www.spadeworx.com
web : www.ormux.com
************************************************************


# re: What is LINQ? 12/30/2007 6:38 PM suyog.kale
very good article on Microsoft new baby :: LINQ,
really helping me , must be .net developers also
Thanks & Rgds,
************************************************************
Suyog Kale
Software Programmer
SpadeWorx Software Services
302, Sai APEX, Dutta Mandir Chowk, Viman Nagar, Pune - 411 014, India

DID : +91-204-010-0506
Mobile : +91-98600-22-945
email : suyog.kale@spadeworx.com
web : www.spadeworx.com
web : www.ormux.com
************************************************************

# re: What is LINQ? 1/1/2008 7:24 PM Vivek uprit
its a realy ultimate topic on LINQ and thanks to the team of microsoft to provide such a nice concept


# re: What is LINQ? 1/8/2008 9:59 PM Vijay KUmar
GOOD ONE... Its Give the Basic Idea About LINQ But .. may can give Detailed Example THat might Help Lot of New Users of .Net 2008

# re: What is LINQ? 1/11/2008 12:05 AM Hemant Ahire
Its really a nice stuff...
addressing the basic role of LINQ.
Lot of Thanks

# re: What is LINQ? 1/18/2008 12:38 AM Ahsan Riaz
No one answered AM Matt question

How in the heck does the sum() in snippet #2 output 1,2 ??
Can ANy one tell.
Saqib if you can please

# re: What is LINQ? 2/1/2008 10:05 AM Chad Hynes
I'm fairly certain that the sum() in snippet #2 should output one line, 14. It's likely just a mistake made by copy-paste from the original snippet.

# re: What is LINQ? 2/4/2008 8:03 PM Radheshyam Rathi
This is really nice article with good examples.

Thank you.

# re: What is LINQ? 2/6/2008 12:02 AM Asem
Great link.........

Thanks a lot....

Asem Ibohal

# re: What is LINQ? 3/5/2008 11:12 PM Trilok
superb intro to linq

# re: What is LINQ? 3/21/2008 6:50 AM amitabh
nice stuff to give a brief understanding of LINQ

# re: What is LINQ? 3/26/2008 8:44 AM Ihab
Very good. It gave me an overview on Linq. Thank You

# re: What is LINQ? 3/28/2008 10:42 PM Ahsan
Thanks a lot.very nice article on Linq.

# re: What is LINQ? 4/23/2008 7:24 PM Debasmit Samal
Thanks alot.

really it helped me a lot and lot

# re: What is LINQ? 4/27/2008 12:06 AM princy verma
its a gud article n helpd mee a lot
plz can u give an article on latest version of .net also?

# re: What is LINQ? 4/28/2008 7:10 PM Suby
Absolutely brilliant

# re: What is LINQ? 5/14/2008 3:47 PM Muthukumaran
Superp introduction

# re: What is LINQ? 5/18/2008 10:01 PM Jothibasu
It is really nice Article

# re: What is LINQ? 5/22/2008 9:00 PM Mahammed Ali
Introduction of LINQ is very nice.



# re: What is LINQ? 6/2/2008 11:25 PM Nushrat Ali
Hi Folks
this above example helps me a lot to understand about hte LINQ

i am heartly thank full to you

thanks a lot

Nushrat Ali
Sansoft IMS India

# re: What is LINQ? 6/9/2008 5:02 PM Icasper
great example, but the code snippets is a little strange from the basic sql syntax i know, whew!

# re: What is LINQ? 6/9/2008 8:13 PM GuillAbegoon
How soon this LINQ technology will boom. I like this technology I hope this could also be available in VS2005

# re: What is LINQ? 6/30/2008 8:00 PM Kishaloy
Really nice article dude. It helped me a lot to get a overview of LINQ.

# re: What is LINQ? 7/3/2008 9:01 PM Deepak & Sachin
Man Hi man me sayad har developers yeisha hi kuchha khoj
rahethe jo Micosoft New Baby Ko Birth Dekar Pura Kiya,
And
Thanks saquib Bhai For Brilliant Articles
////////////////////////////////////////////////////////////////////////
Deepak sah & Sachin Sah
Software Programmer
Salleri Technology Inc.
Aadarshnagar,2nd floor Runghta Market Birgunj,Nepal

Mobile : +977-9806882802
email : deepak_niitbrj@yahoo.com/deepak@usnet.com.np
web : www.salleri.com.np
web : www.niitbrj.com.np
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

# re: What is LINQ? 7/11/2008 10:55 PM RevathiPrasannaa
Very nice article
Thanks a lot

# re: What is LINQ? 8/4/2008 1:03 PM Nushrat Ali
Hey Jothibasu.
Zangatang?

# re: What is LINQ? 8/19/2008 12:15 AM Pushpa Latha
This link is very useful.
Its very simple to undestand.
Now, I have got the idea about the LINQ.

# re: What is LINQ? 8/19/2008 1:36 AM Kiran Kurapaty
Thanks for sharing the information. Its really very useful to understand the basics.

# re: What is LINQ? 9/4/2008 4:03 AM Kamlaesh
A good Article to start LINQ

Regards,
Kamalesh

# re: What is LINQ? 9/13/2008 5:51 PM Mp3 Film Program Download
Thanks 4

# re: What is LINQ? 9/16/2008 3:20 AM Priyanka Chandok
Nice explanation.
Can u send me more details about LINQ.
That is, Its connectivity with ADO.net and XML with examples.
I wil be very thankful

# re: What is LINQ? 10/7/2008 1:48 AM Mehdi Danesh
Thank you for a very nice atricle regarding LINQ, it helped me to undrestand what LINQ is. The examples you provided demonstrates data populated within an array. Can you provide an example pulling data from SQLServer?
an example comparison between doing a traditional ADO VS LINQ?

Thanks again
M. Danesh

# re: What is LINQ? 10/13/2008 5:25 PM pramod
this gives me basic information of LINQ
but i wanna to
that how it will work with asp.net

# re: What is LINQ? 10/30/2008 5:56 PM cpastudent
Is the result of Snippet #2 14?

# re: What is LINQ? 11/3/2008 6:16 PM Madhu Agarwal
Its a nice article...LINQ seems to have great powers... we would like to know the internal processing of LINQ and perfomance effects..Thanks

# re: What is LINQ? 11/5/2008 6:45 PM Pramod Pandurang Gavate
Nice article to get in LINQ

# re: What is LINQ? 11/18/2008 4:51 PM rajeesh
very useful article.It gives good introduction to LINQ.

# re: What is LINQ? 11/21/2008 1:05 AM sarath
hi men hats off to you....
i got what i wanted from your article ...
thanks

# re: What is LINQ? 12/14/2008 7:22 PM vijay
Very Nice basic Article for new beginner.

# re: What is LINQ? 12/15/2008 8:09 PM jobin
good article...its help more

# re: What is LINQ? 12/15/2008 8:10 PM jobin
nice one

# re: What is LINQ? 12/28/2008 10:52 PM Shaheel
Cool explanation.
Can u send me more details about LINQ.
That is, Its connectivity with ADO.net and XML with examples.
I will be very thankful

# re: What is LINQ? 12/29/2008 5:04 PM skeptic
Why on earth would you load up all your objects into a List and THEN filter/sort them? Seems like LINQ is trying to do the database's job for it, which can only lead to poor performance and bad architecture (functionality in the UI instead of the back end).

# re: What is LINQ? 12/29/2008 11:20 PM Raju
Really Good man.....But you said "In my up coming post you will see both LINQ to SQL and LINQ to XML code snippets. "
we are waiting for you reply...

# re: What is LINQ? 1/20/2009 7:30 PM kalyan
Thanks you so much..
Now I have a clear idea on LINQ.

# re: What is LINQ? 2/10/2009 6:56 PM Muhammad Shoaib Siddiqui
Thanks so much...
Its really great article in LINQ. This article shows the power of LINQ.



# re: What is LINQ? 2/24/2009 6:00 PM Kiran
I Don't Know More Information Plz Send Me Email on my
Email ID.

# re: What is LINQ? 2/25/2009 6:05 PM suresh
Thanks alot for posting such a nice article about LINQ.
As of now i clearly understand what exactly LINQ and how to use it.Can u post some of examples which are related for realtime senarios.

thanks in advance


# re: What is LINQ? 2/27/2009 10:56 PM www.WatchTechVideos.com
Very Good Writeup about LINQ... Sample programs are simple but explains us more about LINQ...

You may be interested in this video which explains more about LINQ
http://wtv.watchtechvideos.com/topic180.html


Regards,
www.WatchTechVideos.com

# re: What is LINQ? 3/5/2009 1:44 AM Hajan Selmani
Great Article.
Very simple and good explained.
-----------------------------------------------
Hajan Selmani, BSc
Application Developer
EduSoft, IT Engineering
Stiv Naumov 1/1-1 , Skopje, R. Macedonia
tel. +389 2 3127323 ; +389 2 3128462
fax. +389 2 3215414
hajan@edusoft.com.mk
www.edusoft.com.mk

# re: What is LINQ? 3/7/2009 2:34 PM Armando J. Amor Jr.
I think link would be the easiest way for me to write more difficult code unlike the old way of programming language. Hope microsoft can more documentations about this. Thank you very much.

# re: What is LINQ? 3/20/2009 12:12 AM Bhup
Thanks alot......
i am an indian say thanks in hindi--"bahut badiya DADA"

# re: What is LINQ? 3/29/2009 3:18 AM Naveen Kumar Mishra
It is good affert

Nice

Good
thanks
Naveen Mishra
Syatem Analyst
Infoedge Solutions Pvt Ltd.
naveen@infoedgesolutions.com

# re: What is LINQ? 4/15/2009 6:28 PM Zulqarnain Satti
Good Article. You explain LINQ very well. Thanks alot and keep it up...TC

# re: What is LINQ? 5/4/2009 4:19 AM Narsimha



this is very good.

Thank you..

with Regards
Narsimha

# re: What is LINQ? 5/5/2009 5:56 AM sanjeeb
Hi

very nice topic.

# re: What is LINQ? 5/10/2009 10:36 PM Raj
What an article!!!! :) It rocks

# re: What is LINQ? 5/17/2009 7:38 PM nazi
it was very helpful to me.......... thank u a lot........

# re: What is LINQ? 5/25/2009 6:07 PM Arvind
really very good article.
thanks

# re: What is LINQ? 6/1/2009 12:34 AM jeneesh
Very nice article...

Thanks a lot....my dear friend.

# re: What is LINQ? 7/17/2009 1:35 AM Sami
Excellent !
I am newbie to LINQ and have path now.

# re: What is LINQ? 7/21/2009 6:49 AM vijay
Very nice one

# re: What is LINQ? 8/10/2009 12:57 AM Yasir Akram
Excellent !
Very good introduction to LINQ

# re: What is LINQ? 8/13/2009 2:05 AM Rajesh Kardile
Good
Best
Better

# re: What is LINQ? 8/19/2009 6:43 AM Unmesh Takalkar
Very Nice article

# re: What is LINQ? 8/29/2009 9:21 AM Rajneesh Hajela, Mumbai
LINQ defines a set of query operators that can be used to query, project
and filter data in arrays, enumerable classes, XML, relational database,
and third party data sources.

# re: What is LINQ? 8/30/2009 8:19 AM fakhrad
Very nice Article About Linq Tanx

# re: What is LINQ? iam madduri srinivas yadav 9/2/2009 3:40 AM madduri srinivas yadav
Thanks for giving some ideas about linqs


# re: What is LINQ? 9/23/2009 6:58 AM Lavanya
Nice article which gives a good starting knowledge for introduction into Microsoft LINQ.

I need more links on latest features on .Net Framework.


Thanks,
Lavanya

# re: What is LINQ? 9/29/2009 10:58 AM PinkFloyd43
linq, wtf, when would I want to do this crap!

# re: What is LINQ? 10/23/2009 11:53 AM Preethi
Very good article...Good work

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: