Applies To: .NET Framework 3.0, LINQ
Hello all, I have already discussed about the LINQ model architecture and the query process for .NET object in my previous posts. But how about the object written by you? Most of the time we write our own object and return the collection. LINQ is applicable for any collection that implements the IEnumerable<T> e.g., Generics. Here as an example
I have defined a very simple customer class
class Customer
{
private int _CustID;
public int CustID
get { return _CustID; }
set { _CustID = value; }
}
private string _CustName;
public string CustName
get { return _CustName; }
set { _CustName = value; }
private int _CustAge;
public int CustAge
get { return _CustAge; }
set { _CustAge = value; }
public Customer(int intCustID, string strCustName, int intCustAge)
_CustID = intCustID;
_CustName = strCustName;
_CustAge = intCustAge;
Then I will create a Generic collection out of this class and execute the LINQ. Lets see how we can do that.
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace LINQConsoleApplication_Object
class Program
static void Main(string[] args)
List<Customer> customers = GetCustomers();
var q = from c in customers
where c.CustAge > 20
select c;
Console.WriteLine("Results");
Console.WriteLine("==========================");
foreach(var val in q)
Console.WriteLine("[{0}] {1} : {2}", val.CustID, val.CustName, val.CustAge);
Console.ReadKey();
/// <summary>
/// Create the Generic collection
/// </summary>
/// <returns></returns>
static List<Customer> GetCustomers()
List<Customer> custs = new List<Customer>();
custs.Add(new Customer(1, "A", 10));
custs.Add(new Customer(2, "B", 20));
custs.Add(new Customer(3, "C", 30));
custs.Add(new Customer(4, "D", 40));
custs.Add(new Customer(5, "E", 50));
custs.Add(new Customer(6, "F", 10));
custs.Add(new Customer(7, "G", 20));
custs.Add(new Customer(8, "H", 30));
custs.Add(new Customer(9, "I", 40));
custs.Add(new Customer(10, "J", 50));
custs.Add(new Customer(11, "K", 60));
custs.Add(new Customer(12, "L", 20));
custs.Add(new Customer(13, "M", 30));
custs.Add(new Customer(14, "N", 40));
custs.Add(new Customer(15, "O", 10));
return custs;
The out put will look like
Results
==========================
[3] C : 30
[4] D : 40
[5] E : 50
[8] H : 30
[9] I : 40
[10] J : 50
[11] K : 60
[13] M : 30
[14] N : 40
LINQ (Language INtegrated Query) it is unified programming model for any kind of data in a data-centric application. Be it Database, XML or Object, this uses same query language throughout. This query language or set operation is integrated in the .NET ecosystem as natural expression language. This concept is based on more declarative programming approach. This is to give instruction to the execution about what you want to be done but not too much about how you want it to be done. This gives immense flexibility to developers to think about the business requirements than to think about the implementation of their coding logic. Also it allows us write our own code if we wish to. So it is not just a readymade package. Everything is done in memory like hash join, expression tree building etc.
How it works:
Suppose you are writing code in LINQ like
var emp = from e in Employee
where e.Age == 30
select e.Name;
Internally it translates to method call like
var emp = Employee.Where(e=> e.Age == 30).Select(e.Name)
This creates an expression tree for all OPERATORS like Select, SelectMany, Take, Skip, TakeWhile, SkipWhile, Where, OrderBy, OrderByDescending, ThenBy, Reverse, Distinct, Union, Intersect, Except, First, FirstDefault, Range, Repeat, Min, Max, Sum, Average, Count are name to few.
But this depends on the Object (here Employee) what it is implementing.
Mainly LINQ expression builder implements two main interfaces
IEnumerable<T>: Is the implementation of LINQ to XML (XLinq).
IQueryable<T>: Implementation is based on
Depending on the type of implementation the LINQ query processor returns the object that holds the expression tree and figure out how to execute and deliver the optimized result set.
In case of database interaction it turns the expression into SQL statements and then fetches back the rows to turn them into the object (in this case “Employee”).
All of them are tightly checked by the compiler and compiler will make sure of syntax etc.
The most interesting part is that we can implement IQueryable<T> over IEnumerable<T>. What it will do is basically create the expression dynamically at runtime and represent the result in IEnumerable<T>. During the execution it returns the IL and executes the result in form of object.
The best example of this is DataSet representation on LINQ. DataSet is in memory representation of data and not SQL database (or no database engine). But the inline architecture works like SQL database. You have index, primary key, relationship etc are available there. Even you can use small filter option in DataSet. It will effectively will implement IEnumerable<T> and will return value to object.
That’s how the LINQ works as per my understanding.
Ref:
Took help from many BLOGS and articles and I cannot remember all of them now. But I must request you to visit this Channel 9 video of Anders Hejlsberg and Sam Druker.
Applies To: .Net Framework 3.0
Another amazing gift from .NET is the .NET Language Integrated Query, popularly known as LINQ. The home page for LINQ has lots of resources http://msdn.microsoft.com/data/ref/linq/. If you download the Microsoft Visual Studio Code Name “Orcas” - LINQ CTP (May 2006) you will get the documents from Microsoft and the Hands on Lab. But to mention you here is that Suppose add-in still does not have the hundred percent IDE support with Visual Studio 2005. So the editor will give you the syntax warning but still your code will run after you compile it.
I have tried couple of the hands on in my PC and would like to share with you. Suppose you have an array containing series of integers and you would like to retrieve the even numbers from there. It is a matter of single line only. Here we go,
namespace LINQConsoleApplication_CSharp
NumQuery();
static void NumQuery()
var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
//Query with Lambda expression
var evenNumbers =
from p in numbers
where (p % 2) == 0
select p;
Console.WriteLine("Result");
foreach(var val in evenNumbers)
Console.WriteLine(val);
Output would be something like (in console)
Result
2
4
6
8