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 Interface3. Object and Collection Initializers.4. Extension methods5. Anonymous Types6. Lamda Expressions7. 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)}

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 :

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 :

This will concat categories of Entertainment and Food. Distinct oprator can also be used to evaluate only distinct elements in resultset.OrderBy / ThenByOrderby/ThenBy can be used to order dataresults.

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 employeeswhere emp.age >65 //Normal Where clause works on all itemsgroup emp by emp.age into grwhere gr.Key > 40 // Grouped where clause similar to Having Clauseselect 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.

Blog Subscription

Learn MVC 5 step by step

My friend Shivprasad Koirala who is also a Microsoft ASP.NET MVP has released Learn MVC 5 step by step video series. It starts right from basics of MVC and goes to a level until you become a professional.
You can start taking the course for free using the below youtube video.
Please try it, you will find it awesome.

My Awards

Hit Counter

Twitter

Best .NET 4.5 Expert CookBook

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!
Another book on .NET 4.5 has been released very recently. It covers Debugging, Testing, Extensibility, WCF, Windows Phone, Windows Azure and many more...
Grab it now !!!GET ANY BOOK AT $5 from PacktPub. Offer is limited