Pages

Thursday, February 26, 2009

Linq Basics

Hi Folks,
Hope you all are enjoying this blog. Its a long time since I last posted in this blog. As technology is getting richer and richer, I must write something new in this post. Right now it is definitely a phase of all of us to take the new .NET framework 3.5 with having lots of flexibility in both coding as well as technology.
Lets start with Whats New in 3.5?

You know the three concepts that was added with .NET 3.0 :
1. Windows Communication Foundation (.NETs first attempt to merge all the existing remoting concepts into a single Service Oriented Architecture).
2. Windows Presentation Foundation (Improvement in presentation layer so that user interface could be enhanced very easily.
3. Workflow foundation ( Easily create workflows to generate business logics, Managing object lifecycles / persistent object storage )

After that after the introduction of .NET 3.5 language enhancements are also made so that programmers could make use of technology more easily to enhance their code easily. The recent changes to C#.NET are :
1. LINQ ( Language Integrated Query)
2. Implicitly Typed Interface
3. Object and Collection Initializers.
4. Extension methods
5. Anonymous Types
6. Lamda Expressions
7. Auto implemented properties.

LINQ
Linq is the Microsoft's first attempt to integrate queries into language. We know, it is really easy to find data from sql objects simply writing a query while its somewhat hectic when we want to do the same thing in a DataTable or Lists. Generally we will have to loop through every elements to find the exact match, if there is some aggregation we need to aggregate the values etc. Linq provides an easy way to write queries that can run with the in memory objects. Let us demonstrate that :

Suppose we want to find all the employees whose age is above 50. From database if we want to do this we would write :
select * from employees where age > 50
In case of doing this from code, let us suppose we have a list of employees which have a property age in it. Now before linq we will write :
List<employee> filteredList = new List<employee>();
foreach(Employee emp in employees) // where employees is a list
{
If(emp.Age > 50)
filteredList.add(emp)
}


If we use linq we would write like :
List<employee> filteredList = new List<employee>();
var filterEnumerable = from emp in employees
where emp.Age >50 select emp;
filteredList = filterEnumerable.ToList();


Thus we see how easy to write a linq expression. Let us demonstrate it a bit more.

From emp in employees : This is the temporary elements for each query loop. By from emp we mean we are creating a var of emp and set the objects that comes from employees list one after another.
where emp.Age >50 select emp: means we are imposing restriction on the emp, so that if its age is more than 50 then select emp.

Lambda Expressions:
C# also provides lamda expressions to have short hand writing of linq expressions using Extension methods to static Enumerable Class. This extension methods are added to every Enumerable objects.
We can write the same filterList Linq Expression using Lamda Expression like this :
var filterEnumerable = employees.Where<employee>(emp => emp.age > 50);
For every linq expression there is a corresponding Lambda expression.

What is VAR?
Var is implicitly typed Interface which comes very handy in case of anonymous types. Suppose we write like:
var customers = from c in customersjoin o in orders on c.CustomerIDequals o.CustomerID into cofrom o in co.DefaultIfEmpty(emptyOrder)select new { c.Name, o.OrderDate, o.Total };
This returns a new type object to an enumerable lists.

Every linq statements returns an IEnumerable list of objects. You may now think what var signifies. Actually it is a implicitely typed interface. It assigns its type based on the object it finds. Thus if we set var i= 10 it will signify that variable i is an Integer variable.

Now let us delve more into Linq with examples, Linq comes with lots of Operators:
  • Restriction operators
  • Projection operators
  • Partitioning operators
  • Join operators
  • Concatenation operator
  • Ordering operators
  • Grouping operators
  • Set operators
  • Conversion operators
  • Equality operator
  • Element operators
  • Generation operators
  • Quantifiers
  • Aggregate operators
Now Let us take each of them one by one.
First I have created three lists :
List employees = new List();
List employees1 = new List();
List orders = new List();

employees.Add(new Employee { age = 40, name = "Basob" });
employees.Add(new Employee { age = 34, name = "Abhishek" });
employees.Add(new Employee { age = 65, name = "Souvik" });
employees.Add(new Employee { age = 65, name = "Ayan" });
employees.Add(new Employee { age = 68, name = "Raj" });
employees1.Add(new Employee { age = 68, name = "Pallab" });
employees1.Add(new Employee { age = 55, name = "Swarup" });
employees1.Add(new Employee { age = 68, name = "Ranjit" });
employees1.Add(new Employee { age = 68, name = "Bratin" });
orders.Add(new Order { empName = "Raj", itemName = "Pen" });
orders.Add(new Order { empName = "Souvik", itemName = "Pencil" });
orders.Add(new Order { empName = "Raj", itemName = "Rubber" });
Now Let us use these lists to demonstrate each of the operators.

Restriction Operator:
Restriction operator can be applied by using Where clause. Example of Where clause:
var filterEnumerable = from emp in employeeswhere emp.age > 50select emp;ORvar filterEnumerable = employees.Where<employee>(emp => emp.age > 50);

This filters out Employees by age greater than 50.

Projection Operator:
With the word projection, I mean Select statements. Every linq elements should have projection in it.
var iNames = from i in employees select i.name;ORvar iNames = employees.select<employee,string>
Here IEnumerable of Name is returned.

Partitioning using Take /Skip operators
Take can be used when we take first N elements in a list, skip will take the elements after N.
var MostAged2 = employees.OrderByDescending(i =>i.age).Take(2);
var AllButMostAged2 =employees.OrderByDescending(i => i.age).Skip(2);

Takewhile and skipwhile operator will select from a list based on a delegate passed in.

var allWithfourwordlength = employees.SkipWhile<employee>(r => r.name.Length > 4);


Join Operators:
Join operators have 2 parts. The outer part gets results from inner part and vice versa so returns the result based on both
var filterEnumerable = from emp in employeesjoin ord in orders on new { Name = emp.name }equals new { Name = ord.empName }select emp;ORvar filterEnumerable = employees.Join<employee,Order, string, Employee>(orders, e1 => e1.name,o => o.empName, (o, e2) => o);
Here we are joining employees and order based on the names passed in

Concatenation Operator :
The Concatenation operator concats two sequence.
var items = ( from itEnt in _itemListwhere itEnt.Category.Equals("Entertainment")select itEnt.ItemName).Concat(from it2 in _itemListwhere it2.Category.Equals("Food")select it2.ItemName).Distinct();
This will concat categories of Entertainment and Food. Distinct oprator can also be used to evaluate only distinct elements in resultset.
OrderBy / ThenBy
Orderby/ThenBy can be used to order dataresults.

var orderItems = from emp in employees orderby emp.name,emp.age descending;
var orderItems =employees.OrderBy(i => i.name).ThenByDescending(i => i.age);
Here the ordering is done by name and then decending by age.

GroupBy Operator :

This is used to group elements.
var itemNamesByCategory =from i in _itemListgroup i by i.Category into gselect new { Category = g.Key, Items = g };
This gets all the categories and items grouped by category. Well this grouping seems to be a little tricky for me. Let me make you understand what exactly is the way. Here while we are grouping, we are taking the group into g(which is a IGrouping). The g will have a key, which holds the grouped data, you can add multiple grouping statement. If you want to have a having clause, its just the where clause will do. Just like the example below:
var filterEnumerable2 = from emp in employees
where emp.age >65 //Normal Where clause works on all items
group emp by emp.age into gr
where gr.Key > 40 // Grouped where clause similar to Having Clause
select new { aaa = gr.Key, ccc=gr.Count(), ddd=gr.Sum(r=>r.age), bbb = gr };
Here in the example, I have returned the aggregate functions like sum, count etc which you can find from group data.

Distinct / Union / Intersect / Except Operators :

Union operator produces an union of two sequences
var un = (from i in _itemListselect i.ItemName).Distinct().Union((from o in _orderListselect o.OrderName).Distinct());
Intersect operator produces an intersection of two sequences.
var inter = (from i in _itemListselect i.ItemID).Distinct().Intersect((from o in _orderListselect o.OrderID).Distinct());
Except operator produces a set of difference elements from two sequences.
var inter = (from i in _itemListselect i.ItemID).Distinct().Except((from o in _orderListselect o.OrderID).Distinct())

Well there are lots more to tell you about LINQ. I am stopping it here. If you want more,
Read the Full Article.

No comments:

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.