Introduction To linq

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








Up Next
    Ebook Download
    View all
    Learn
    View all