Hi Friends, Its a long time since I have last written a Article. Since then, I have learned a lot of things and its time to share them with you. .NET is getting richer and richer with new concepts. The most popular amongst all of them is the introduction of Query Integration on basic Application Data.

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.

Linq is always associated with Lambda Expressions. In .NET 2.0 we have the concept of Annonymous Methods that allows you to write function body inline without the need of writing a delegate function. Lambda Expression of .NET 3.5 is to consize the concept of Annonymous function writing. Let us take a look of how to write Annonymous methods in C# 2.0.

In the 2nd Case we can see that just declaring a Delegate and giving instance of that delegate type we can easily manage to write annonymous methods. .NET 3.5 puts forward this concept a little more compact. We can take the use of => operator which is introduced in .NET 3.5. It gives more flexible and easier way to write expression. Lambda Expressions can be used in the prevous case to get the result like this:

Thus the delegate type is assigned directly. The meaning of the line value =>value + 2 is just similar to declaring a function. The first value indicates the arguments that we pass in a function and the one after the "=>" operator is the body of the function. Similarly if we want we can feed in as many arguments as we want to. Just in the following examples :

In this example we passed in 2 arguments to the function and returns the result. You may also wonder how can I write a lambda expression when your function body takes more than one expression. Dont worry, its simple. Take a look at the following example.

Thus we see writing Lambda expression is very easy. This comes very handy when working with linq, as most of the extension methods that are associated with Linq takes function delegates as arguments. We can pass simple delegate function body easily to those functions to perform work easily.

These type of expressions are also called as Expression Trees as the basic unit of this expressions are data and structured in tree format. We can also create dynamic Expression types ourself if we like to using the Expression Class.

Another new concept that comes with .NET 3.5 is the Extension methods. Now we can include our own custom methods in already defined objects. We can create static classes and include custom methods to objects. Extension method behavior is similar to that of static methods. You can declare them only in static classes. To declare an extension method, you specify the keyword this as the first parameter of the method. Let us look at the following example:

If we include the namespace to our application, any string variable will have ToInt32Extension method. This function takes 0 arguments and passes the string to s. We can also pass parameters just like below:

We can also create ExtensionMethod that takes function as argument. The class Enumerable extends a number of methods which makes use of Func class to send a method delegate within the extension so that it could work with the IEnumerable list of objects. Let us create our own extension method that can work with IEnumerable as well, just look at the code below:

This will call the function predicate that we pass into the function, and if the function returns true, it will grant it. Thus we make a with return of a list of source similar to Where clause as that is defined in Enumerable class as extension method.

You may notice here I am using yield keyword to generate the list of items. This is one of the new addition to C# 2.0. The meaning of this keyword is to instruct the program to run and generate the list until all the loop execution is exhausted. Thus it creates a list of items for which the predicate function returns true.

FOR VB.NET developersIn case of VB.NET we have to define the extension method inside a Module with Attribute<System.Runtime.CompilerServices.Extension()>.

var is very useful when working with annonymous typed just introduced. It assigns the type of the variable based on the type of the object passed in, so there is no difference of doing List<Employee>() x = new List<Employee>() and doing var x = new List<Employee>() as in both the case the type of the variable x is always get to List<Employee>.

var itemNamesByCategory = from i in _itemList
group i by i.Category into g
selectnew { Category = g.Key, Items = g };

VB.NET
Dim itemNamesByCategory = From i In _itemList _
Group i By i.Category _
Into g _
Select g.Key

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<tsource,telement>). 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:

DLINQ or LINQ to SQL provides a runtime framework to work with Relation Database objects. It translates LINQ queries into SQL queries automatically. We can create the total snapshot of the database using LINQ objects so that the entire relationship can be created in application classes and objects. There are tools that can create those objects automatically, we can make use of them in our program. We can make use of DataContext classes to get the flavour of DLINQ. .NET makes use of enhanced Attributes to define classes.

Similar to DLINQ, XLINQ means using LINQ to XML documents. .NET extended the little jem LINQ to create a number of classes which can manipulate XML documents very easily. Compared to XML DOM, XLINQ is much more flexible because it does not require you to always have a document object to be able to work with XML. Therefore, you can work directly with nodes and modify them as content of the document without having to start from a root XmlDocument object. This is a very powerful and flexible feature that you can use to compose larger trees and XML documents from tree fragments. Let us look at the following example:

XDocument bookStoreXml =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Bookstore XML Example"),
new XElement("bookstore", new XElement("genre",
new XAttribute("name", "Fiction"),
new XElement("book", new XAttribute("ISBN",
"10-861003-324"), new XAttribute("Title", "A Tale of Two Cities"),
new XAttribute("Price", "19.99"),
new XElement("chapter", "Abstract...", new XAttribute("num", "1"),
new XAttribute("name", "Introduction")),
new XElement("chapter", "Abstract...", new XAttribute("num", "2"),
new XAttribute("name", "Body")
)
)
)
)
);

In this example, we have created an XML document using XDocument class. The XDocument class may contain XElements which are the XML elements.XDeclaration is the class to create XML declaration. XAttribute creates an XML attributes. XComment creates XML comments. In this example Root element bookstore holds all the child elements inside it. We can pass an entire tree of XElement inside any XElement thus it gives easier way to create XML documents.

After reading, creating or manipulating the xml document, we can save the document easily to the disk using save method on XDocument object.

bookStoreXml.Save(@"C:\XMLDocuments\Bookstore.xml");

It will save the entire document to the disk.

To read the document again and create the entire tree structure of object in application end we call the load method of XElement.

XElement.Load(@"C:\XMLDocuments\Bookstore.xml");

We can also use parse method if we want to load the xml from a string.

You can query XML document in the same way as we use in objects. We can make use of some methods Ancestors (which returns all the ancestor elements in IEnumerable list), Descendants (returns all the decendants of the element), Element(returns the child element with name), Elements(returns all the child elements of the current node), Nodes (returns the content of the current node)

Share

About the Author

Oh, lets go a bit further to know him better. Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well. Have any problem? Write to him in his Forum.