Saqib Ullah

BootStrapper Know How

  Home  |   Contact  |   Syndication    |   Login
  91 Posts | 1 Stories | 226 Comments | 16 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? 12/30/2007 6:39 PM suyog.kale
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
o LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
o LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
o 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


Very Helpful links,

http://geekswithblogs.net/technetbytes/archive/2007/04/30/112129.aspx

http://dotnetslackers.com/articles/csharp/IntroducingLINQ1.aspx

http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx


# 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? 4/28/2008 7:11 PM Suby
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/28/2008 11:19 PM sdfs
sdfsdf

# 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? 8/19/2008 2:17 AM John
A good article to start up with linq

# 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

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 3 and type the answer here: