Académique Documents
Professionnel Documents
Culture Documents
Introduction
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
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.
Types of LINQ
Linq Comes with 3 Basic Types (Provided there are lots of more types of LINQ on
different type of objects :
1. LINQ (Linq to Objects)
2. DLINQ (Linq to SQL)
3. XLINQ (Linq to XML)
Before we continue with LINQ, we must look at some of the new inclusions which may
come to us very handy.
Lambda Expression
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.
Hide Copy Code
int i = this.compute(10);
private int compute(int value)
{
return (value + 2);
}
It is very normal example of a function call. The compute function is called when the line
i=this.compute(10) is called. We can replace the above code with inline annonymous
function like this:
Hide Copy Code
//C#
delegate int DelType(int i);
DelType dd = delegate(int value)
{
return (value +2);
};
int i = dd(10);
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:
Hide Copy Code
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 :
Hide Copy Code
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.
Hide Copy Code
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.
Hide Copy Code
Thus if we write a function which receives the ExpressionTrees we can decompose the
parameters easily.
NOTE:
VB.NET doesnt support annonymous methods and lambda expressions with statement
body now.
Extension Methods
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:
Hide Copy Code
}
}
Now if I call this function, it will return the Min value. The selector function will be the
annonymous delegate passed in. Thus the selector will call the function like
Hide Copy Code
Thus it will compare all the ages of employees, and gets the minimum of them.
Similar to this, let us write Where function ourself:
Hide Copy Code
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.
What is Yield ?
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.
Annonymous Type
Annonymous types introduced with .NET can create object without even declaring the
classes. The members of the annonymous types are implicitely defined during
compilation
Hide Copy Code
Meaning of VAR
Var is a new keyword added in .NET 3.5. This is called Implicit Type Variable. During
runtime it automatically assigns its type of the variable based on the object if finds.
Hide Copy Code
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>.
Only restriction is we cannot define members of var type.
Demonstration of Linq
Let us now demonstrate LINQ with a simple example. The example can be found on the
source code associated with the article.
First we define
Hide Shrink
C#
List<Employee> employees = new List<Employee>();
List<Employee> employee1 = new List<Employee>();
List<Order> orders = new List<Order>();
//Where Employee and Order are classes.
public class Employee
{
public string name;
public int age;
public override string ToString()
Copy Code
{
return this.name;
}
}
public class Order
{
public string itemName;
public string empName;
public override string ToString()
{
return this.itemName;
}
}
Operators
While writing queries, you will find a number of operators that we can use. They most
general opreators are :
Projection operators (Select)
Restriction operators (Where)
Partitioning operators (Take / Skip, TakeWhile/ SkipWhile)
Join operators (Join)
Concatenation operator (Concat)
Ordering operators (order by)
Grouping operators (group by into)
Projection Operator
With the word projection, I mean Select statements. Every linq elements should have
projection in it.
C#
var iNames = from i in employees
select i.name;
//Using Lambda Expression
var iNames = employees.Select<Employee, string>(r => r.name);
Here IEnumerable of names of employees is returned. In the above example we are also
taking advantage of annonymous type declaration that was introduced in .NET 3.5.
Restriction Operator
Restriction operator can be applied by using Where clause. Example of Where clause:
Takewhile and skipwhile operator will select from a list based on a delegate passed
in.
Hide Copy Code
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 employees
join ord in orders
on emp.name
equals ord.empName
select emp;
//Using Lambda Expression
var 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 filterEnumerable = (from emp in employees
select emp).Concat<Employee>(
from emp in employees1
select emp);
//Using Lambda Expression
var filterEnumerable = employees.Concat<Employee>(
employees1.AsEnumerable<Employee>());
Hide Copy Code
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.
Hide Copy Code
GroupBy Operator :
This is used to group elements.
var itemNamesByCategory = from i in _itemList
group i by i.Category into g
select new { Category = g.Key, Items = g };
Hide Copy Code
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:
Hide Copy Code
ccc=gr.Count<employee>(),
ddd=gr.Sum<employee>(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.
Use of let
We can create temporary variables within LINQ statements using let keyword
Hide Copy Code
Here x and y are intermediate variables and we will get enumerable of integers 5 times
the age of each employees.
DLINQ
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.
Let us take an example
Hide Copy Code
[Table(Name="Employee")]
public class Employee
{
[Column(Id=true)]
public string EmpID;
[Column]
public string Name;
}
This means Employee class corresponds to the actual Employee table. Where EmpID is
the primary key of the table and Name is another column.
For Further Reading:
LINQ to SQL on MSDN
XLINQ
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:
Hide Copy Code
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")),
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)
For Further Reading
XLINQ on MSDN
Reference
For Further reading, you may look into the following links:
1. Charlie Claverts Blog
2. Msdn Article on Linq Expression
3. LINQ Article on MSDN
Conclusion
Therefore using Linq we can easily handle search among complex application data
easily leverage out lots of loops and conditional statements.