SIGN UP MEMBER LOGIN:    
ARTICLE

Introduction To linq

Posted by Rekha Articles | LINQ with C# July 29, 2009
This article is a basic introduction to LINQ.
Reader Level:

What is LINQ?

LINQ (Language Integrated Query) is a Microsoft programming model and methodology that essentially adds formal query capabilities into Microsoft .NET-based programming languages. LINQ offers a compact, expressive, and intelligible syntax for manipulating data. The real value of LINQ comes from its ability to apply the same query to an SQL database, a Dataset, an array of objects in memory and to many other types of data as well.

LINQ uses an SQL-like syntax to make query expressions well beyond the capabilities of embedded SQL as implemented in programming languages. That's because embedded SQL uses a simplified, streamlined syntax to add SQL statements to other programming languages, where there's no attempt to integrate such statements into the native syntax and typing mechanisms. Furthermore, LINQ may be used to access all kinds of data, whereas embedded SQL is limited to addressing only databases that can handle SQL queries.

The official goal of LINQ family of technologies is to add "general purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data".

Advantages of LINQ:

  1. LINQ offers an object-based, language-integrated way to query over data no matter where that data came from. So through LINQ we can query database,XML as well as collections.
  2. Compile time syntax checking
  3. It allows you to query collections like arrays, enumerable classes etc in the native language of your application, like VB or C# in much the same way as you would query a database using SQL

LINQ is basically of three types:

  1. LINQ to Object
  2. LINQ to XML
  3. LINQ to ADO.NET
Here are three simple examples of how to use LINQ with objects, XML and ADO.NET.


1. LINQ to Object

Quering an Object:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Linq1

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] a = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

            var days = from day in a select day;

            foreach (var day in days)

            {

                Console.WriteLine(day);

            }

        }

    }

}


The output looks like this:

1.bmp


2. LINQ to XML

Reading Data from an Array Object and Display it in XML Format:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.Xml.Linq;

 

namespace Linq2

{

    class Book

    {

        public string Book_nm;

        public double Book_Price;

        public int Book_year;

 

        public Book(string book_nm, double book_price, int year)

        {

            Book_nm = book_nm;

            Book_Price = book_price;

            Book_year = year;

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Book[] b1 = new Book[] { new Book("LINQ in C#",560.00,2008),

                new Book("Windows Programming in C#",1100.00,2007) };

 

            XElement x1 = new XElement("books", from book in b1 select new XElement("book",

                new XElement("title", book.Book_nm), new XElement("price", book.Book_Price)));

 

            Console.WriteLine(x1);

        }

    }

}

The output looks like this:


2.bmp


3. LINQ to SQL


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Linq3

{

    class Program

    {

 

        static void Main(string[] args)

        {

            MyLINQDataContext m1 = new MyLINQDataContext();

            var st = from s in m1.students select s;

            Console.WriteLine("\n\tRoll No Name Location \n");

            foreach (var x in st)

            {

                Console.WriteLine("\t" + x.rollno + "\t\t" + x.st_name + "\t\t " + x.st_loc + "\n");

            }

        }

    }

}


The output looks like this:

3.bmp








Login to add your contents and source code to this article
share this article :
post comment
 

This was a simple and good demonstration of LINQ. Happy Coding

Posted by Madhusudhanredd Thotli Sep 07, 2010

XElement x1 = new XElement("books", from book in b1 select new XElement("book",

                new XElement("title", book.Book_nm), new XElement("price", book.Book_Price)));

Posted by Raghavendra u Aug 31, 2009

i can't understand linq.Plz tell me ebook of linq


                                                              thanks.

Posted by Raghavendra u Aug 31, 2009

oh is it.. if so itz sad

Posted by Rekha Aug 05, 2009

I recently read rumors saying that Linq to SQL is moving to "maintenance mode" which means that the dev team will not extend or improve it beyond bug fixes.

Sad indeed, because its a wonderful light weight extensible data access option.


Danny, Los Angeles Locksmith

Posted by danny dror Aug 04, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor