LINQ – Querying the Data

This is one of my posts based on LINQ subject on which I gratefully able to work a lot since last one and half year and I am hoping for myself to have some of good posts on EF & LINQ. In this post, I am going a bit theoretical here.

As the name implies, LINQ is all about querying for the data and retrieving the resultant sequence of the same. Queries which return set of matching objects or subset of fields of matching an object or set of objects. The result or the return set of object by the LINQ is called “sequence”. This “sequence” generally be of type IEnumerable <T> where T is the data type of the object stored in the sequence. For example, if you have a sequence of integers, they would be stored in a variable of type IEnumerable<int>.


LINQ to Objects
LINQ to Objects is the API dedicated to working with querying objects, arrays and in-memory data collections. Example is, querying a string Array.

Example:
string[] greetings = {"Hi Kaushal Parik", "Hi Pooja Parik", "Hi Naman Parik"};
var items =
    from s in greetings
    where s.StartsWith("Naman")
    select s;

foreach (var item in items)
    Console.WriteLine(item);

Result will be: Hi Naman Parik


LINQ to Dataset
LINQ to Dataset is the name given to the LINQ API for Datasets. LINQ to Dataset is the faster and easier way to query cached data in Datasets. This is specifically useful when we need to query on consolidated data from one of more data sources. LINQ to Dataset doesn’t mean to replace ADP.NET 2.0 but its build/extended upon ADO.NET 2.0 architecture. So, existing ADO.NET 2.0 code will continue to run functionally correct in a LINQ to Dataset application.

Example:
DataSet ds = new DataSet();
var employees = from e in ds.Tables[0].AsEnumerable()
                select new
                {
                    eid = e.Field<string>("EmployeeID"),
                    ename = e.Field<string>("FullName"),
                    esalary = e.Field<string>("Salary")
                };


LINQ to SQL
LINQ to SQL is the name given to the IQueryable<T> API that allows LINQ queries to work with Microsoft’s SQL Server database. This requires you to have reference of the System.Data.Linq dll assembly in your project.

Example:
var customers =
    from c in dbContext.Customers
    where c.City.Equals("Ahmedabad")
    select c;

foreach (var cust in custs)
    Console.WriteLine("{0}", cust.CompanyName);


LINQ to XML
LINQ to XML is the name given to the LINQ API dedicated to working with XML. This requires you to have reference of the System.Xml.Linq dll assembly in your project

Example:
using System;
using System.Linq;
using System.Xml.Linq;

XElement customers = XElement.Parse(
    @"<customers>
        <customer>
            <id>1</id>
            <name>kaushal parik</name>
        </customer>
        <customer>
            <id>2</id>
            <name>Pooja parik</name>
        </customer>
        <customer>
            <id>3</id>
            <name>Naman parik</name>
        </customer>
    </customers>");

var names =
    from cust in customers.Elements("customer")
    where (string) book.Element("author") == "Joe Rattz"
    select cust.Element("name");

foreach(var name in names)
    Console.WriteLine(name.Value);


LINQ to Entities
LINQ to Entities is the name given to the LINQ API that is used to interface with a database. LINQ to Entities separates physical database and the entity object model by injecting logical mapping in between them which simply results in greater flexibility.

hope this helps./.

more to come on EF & Linq.

Comments

# Twitter Trackbacks for LINQ ??? Querying the Data - KaushaL.NET [dotnetslackers.com] on Topsy.com

Pingback from  Twitter Trackbacks for                 LINQ ??? Querying the Data - KaushaL.NET         [dotnetslackers.com]        on Topsy.com