Thursday, July 9, 2009

LINQ:Language Integrated Query



Introducing Linq
Linq is short for Language Integrated Query. If you are used to using SQL to query databases, you are going to have something of a head start with Linq, since they have many ideas in common. Before we dig into Linq itself, let's step back and look at what makes SQL different from C#.Imagine we have a list of orders. For this example, we will imagine they are stored in memory, but they could be in a file on disk too. We want to get a list of the costs of all orders that were placed by the customer identified by the number 84. If we set about implementing this in C# before version 3 and a range of other popular languages, we would probably write something like (assuming C# syntax for familiarity): List Found = new List();foreach (Order o in Orders) if (o.CustomerID == 84) Found.Add(o.Cost);
Here we are describing how to achieve the result we want by breaking the task into a series of instructions. This approach, which is very familiar to us, is called imperative programming. It relies on us to pick a good algorithm and not make any mistakes in the implementation of it; for more complex tasks, the algorithm is more complex and our chances of implementing it correctly decrease.If we had the orders stored in a table in a database and we used SQL to query it, we would write something like: SELECT Cost FROM Orders WHERE CustomerID = 84
Here we have not specified an algorithm, or how to get the data. We have just declared what we want and left the computer to work out how to do it. This is known as declarative or logic programming.Linq brings declarative programming features into imperative languages. It is not language specific, and has been implemented in the Orcas version of VB.Net amongst other languages. In this series we are focusing on C# 3.0, but the principles will carry over to other languages.
Understanding A Simple Linq Query
Let's jump straight into a code example. First, we'll create an Order class, then make a few instances of it in a List as our test data. With that done, we'll use Linq to get the costs of all orders for customer 84. class Order{ private int _OrderID; private int _CustomerID; private double _Cost; public int OrderID { get { return _OrderID; } set { _OrderID = value; } } public int CustomerID { get { return _CustomerID; } set { _CustomerID = value; } } public double Cost { get { return _Cost; } set { _Cost = value; } }}class Program{ static void Main(string[] args) { // Set up some test orders. var Orders = new List { new Order { OrderID = 1, CustomerID = 84, Cost = 159.12 }, new Order { OrderID = 2, CustomerID = 7, Cost = 18.50 }, new Order { OrderID = 3, CustomerID = 84, Cost = 2.89 } }; // Linq query. var Found = from o in Orders where o.CustomerID == 84 select o.Cost; // Display results. foreach (var Result in Found) Console.WriteLine("Cost: " + Result.ToString()); }}
The output of running this program is: Cost: 159.12Cost: 2.89
Let's walk through the Main method. First, we use collection and object initializers to create a list of Order objects that we can run our query over. Next comes the query - the new bit. We declare the variable Found and request that its type be inferred for us by using the "var" keyword.We then run across a new C# 3.0 keyword: "from". from o in Orders
This is the keyword that always starts a query. You can read it a little bit like a "foreach": it takes a collection of some kind after the "in" keyword and makes what is to the left of the "in" keyword refer to a single element of the collection. Unlike "foreach", we do not have to write a type.Following this is another new keyword: "where". where o.CustomerID == 84
This introduces a filter, allowing us to pick only some of the objects from the Orders collection. The "from" made the identifier "o" refer to a single item from the collection, and we write the condition in terms of this. If you type this query into the IDE yourself, you will notice that it has worked out that "o" is an Order and intellisense works as expected.The final new keyword is "select". select o.Cost
This comes at the end of the query and is a little like a "return" statement: it states what we want to appear in the collection holding the results of the query. As well as primitive types (such as int), you can instantiate any object you like here. In this case, we will end up with Found being a List, though.You may be thinking at this point, "hey, this looks like SQL but kind of backwards and twisted about a bit". That is a pretty good summary. I suspect many who have written a lot of SQL will find the "select comes last" a little grating at first; the other important thing to remember is that all of the conditions are to be expressed in C# syntax, not SQL syntax. That means "==" for equality testing, rather than "=" in SQL. Thankfully, in most cases that mistake will lead to a compile time error anyway.
A Few More Simple Queries
We may wish our query to return not only the Cost, but also the OrderID for each result that it finds. To do this we take advantage of anonymous types. var Found = from o in Orders where o.CustomerID == 84 select new { OrderID = o.OrderID, Cost = o.Cost };
Here we have defined an anonymous type that holds an OrderID and a Cost. This is where we start to see the power and flexibility that they offer; without them we would need to write custom classes for every possible set of results we wanted. Remembering the projection syntax, we can shorten this to: var Found = from o in Orders where o.CustomerID == 84 select new { o.OrderID, o.Cost };
And obtain the same result. Note that you can perform whatever computation you wish inside the anonymous type initializer. For example, we may wish to return the Cost of the order with an additional sales tax of 10% added on to it. var Found = from o in Orders where o.CustomerID == 84 select new { o.OrderID, o.Cost, CostWithTax = o.Cost * 1.1 };
Conditions can be more complex too, and are built up in the usual C# way, just as you would do in an "if" statement. Here we apply an extra condition that we only want to see orders valued over a hundred pounds. var Found = from o in Orders where o.CustomerID == 84 && o.Cost > 100 select new { o.OrderID, o.Cost, CostWithTax = o.Cost * 1.1 };

Ordering
It is possible to sort the results based upon a field or the result of a computation involving one or more fields. This is achieved by using the new "orderby" keyword. var Found = from o in Orders where o.CustomerID == 84 orderby o.Cost ascending select new { o.OrderID, o.Cost };
After the "orderby" keyword, we write the expression that the objects will be sorted on. In this case, it is a single field. Notice this is different from SQL, where there are two words: "ORDER BY". I have added the keyword "ascending" at the end, though this is actually the default. The result is that we now get the orders in order of increasing cost, cheapest to most expensive. To get most expensive first, we would have used the "descending" keyword.While I said earlier that the ordering condition is based on fields in the objects involved in the query, it actually doesn't have to be. Here's a way to get the results in a random order. Random R = new Random();var Found = from o in Orders where o.CustomerID == 84 orderby R.Next() select new { OrderID = o.OrderID, Cost = o.Cost };

Joins
So far we have just had one type of objects to run our query over. However, real life is usually more complex than this. For this example, let's introduce another class named Customer. class Customer{ private int _CustomerID; private string _Name; private string _Email; public int CustomerID { get { return _CustomerID; } set { _CustomerID = value; } } public string Name { get { return _Name; } set { _Name = value; } } public string Email { get { return _Email; } set { _Email = value; } }}
In the Main method, we will also instantiate a handful of Customer objects and place them in a list. var Customers = new List { new Customer { CustomerID = 7, Name = "Emma", Email = "emz0r@worreva.com" }, new Customer { CustomerID = 84, Name = "Pedro", Email = "pedro@cerveza.es" }, new Customer { CustomerID = 102, Name = "Vladimir", Email = "vladimir@pivo.ru" } };
We would like to produce a list featuring all orders, stating the ID and cost of the order along with the name of the customer. To do this we need to involve both the List of orders and the List of customers in our query. This is achieved using the "join" keyword. Let's replace our query and output code with the following. // Query.var Found = from o in Orders join c in Customers on o.CustomerID equals c.CustomerID select new { c.Name, o.OrderID, o.Cost };// Display results.foreach (var Result in Found) Console.WriteLine(Result.Name + " spent " + Result.Cost.ToString() + " in order " + Result.OrderID.ToString());
The output of running this program is: Pedro spent 159.12 in order 1Emma spent 18.5 in order 2Pedro spent 2.89 in order 3
We use the "join" keyword to indicate that we want to refer to another collection in our query. We then once again use the "in" keyword to declare an identifier that will refer to a single item in the collection; in this case it has been named "c". Finally, we need to specify how the two collections are related. This is achieved using the "on ... equals ..." syntax, where we name a field from each of the collections. In this case, we have stated that the CustomerID of an Order maps to the CustomerID of a Customer.When the query is evaluated, an object in the Customers collection is located to match each object in the Orders collection. Note that if there were many customers with the same ID, there may be more than one matching Customer object per Order object. In this case, we get extra results. For example, change Vladimir to also have an OrderID of 84. The output of the program would then be: Pedro spent 159.12 in order 1Vladimir spent 159.12 in order 1Emma spent 18.5 in order 2Pedro spent 2.89 in order 3Vladimir spent 2.89 in order 3
Notice that Vladimir never featured in the results before, since he had not ordered anything.
Getting All Permutations With Multiple "from"s
It is possible to write a query that gets every combination of the objects from two collections. This is achieved by using the "from" keyword multiple times. var Found = from o in Orders from c in Customers select new { c.Name, o.OrderID, o.Cost };
Earlier I suggested that you could think of "from" as being a little bit like a "foreach". You can also think of multiple uses of "from" a bit like nested "foreach" loops; we are going to get every possible combination of the objects from the two collections. Therefore, the output will be: Emma spent 159.12 in order 1Pedro spent 159.12 in order 1Vladimir spent 159.12 in order 1Emma spent 18.5 in order 2Pedro spent 18.5 in order 2Vladimir spent 18.5 in order 2Emma spent 2.89 in order 3Pedro spent 2.89 in order 3Vladimir spent 2.89 in order 3
Which is not especially useful. You may have spotted that you could have used "where" in conjunction with the two "from"s to get the same result as the join: var Found = from o in Orders from c in Customers where o.CustomerID == c.CustomerID select new { c.Name, o.OrderID, o.Cost };
However, don't do this, since it computes all of the possible combinations before the "where" clause, which goes on to throw most of them away. This is a waste of memory and computation. A join, on the other hand, never produces them in the first place.
Grouping
Another operations that you may wish to perform is categorizing objects that have the same value in a given field. For example, we might want to categorize orders by CustomerID. The result we expect back is a list of groups, where each group has a key (in this case, the CustomerID) and a list of matching objects. Here's the code to do the query and output the results. // Group orders by customer.var OrdersByCustomer = from o in Orders group o by o.CustomerID;// Iterate over the groups.foreach (var Cust in OrdersByCustomer){ // About the customer... Console.WriteLine("Customer with ID " + Cust.Key.ToString() + " ordered " + Cust.Count().ToString() + " items."); // And what they ordered. foreach (var Item in Cust) Console.WriteLine(" ID: " + Item.OrderID.ToString() + " Cost: " + Item.Cost.ToString());}
The output that it produces is as follows: Customer with ID 84 ordered 2 items. ID: 1 Cost: 159.12 ID: 3 Cost: 2.89Customer with ID 7 ordered 1 items. ID: 2 Cost: 18.5
This query looks somewhat different to the others that we have seen so far in that it does not end with a "select". The first line is the same as we're used to. The second introduces the new "group" and "by" keywords. After the "by" we name the field that we are going to group the objects by. Before the "by" we put what we would like to see in the resulting per-group collections. In this case, we write "o" so as to get the entire object. If we had only been interested in the Cost field, however, we could have written: // Group orders by customer.var OrdersByCustomer = from o in Orders group o.Cost by o.CustomerID;// Iterate over the groups.foreach (var Cust in OrdersByCustomer){ // About the customer... Console.WriteLine("Customer with ID " + Cust.Key.ToString() + " ordered " + Cust.Count().ToString() + " items."); // And the costs of what they ordered. foreach (var Cost in Cust) Console.WriteLine(" Cost: " + Cost.ToString());}
Which produces the output: Customer with ID 84 ordered 2 items. Cost: 159.12 Cost: 2.89Customer with ID 7 ordered 1 items. Cost: 18.5
You are not restricted to just a single field or the object itself; you could, for example, instantiate an anonymous type there instead.
Query Continuations
At this point you might be wondering if you can follow a "group ... by ..." with a "select". The answer is yes, but not directly. Both "group ... by ..." and "select" are special in so far as they produce a result. You must terminate a Linq query with one or the other. If you try to do something like: var CheapOrders = from o in Orders where o.Cost < ordercounts =" from" customerid =" g.Key," totalorders =" g.Count()">Under The Hood
Now we have looked at the practicalities of using Linq, I am going to spend a little time taking a look at how it works. Don't worry if you don't understand everything in this section, it's here for those who like to dig a little deeper.Throughout the series I have talked about how all of the language features introduced in C# 3.0 somehow help to make Linq possible. While anonymous types have shown up pretty explicitly and you can see from the lack of type annotations we have been writing that there is some type inference going on, where are the extension methods and lambda expressions?There's a principle in language design and implementation called "syntactic sugar". We use this to describe cases where certain syntax isn't directly compiled, but is first transformed into some other more primitive syntax and then passed to the compiler. This is exactly what happens with Linq: your queries are transformed into a sequence of method calls and lambda expressions.The C# 3.0 specification goes into great detail about these transformations. In practice, you probably don't need to know about this, but let's look at one example to help us understand what is going on. Our simple query from earlier: var Found = from o in Orders where o.CustomerID == 84 select o.Cost;
After transformation by the compiler, becomes: var Found = Orders.Where(o => o.CustomerID == 84) .Select(o => o.Cost);
And this is what actually gets compiled. Here the use of lambda expressions becomes clear. The lambda passed to the Where method is called on each element of Orders to determine whether it should be in the result or not. This produces another intermediate collection, which we then call the Select method on. This calls the lambda it is passed on each object and builds up a final collection of the results, which is then assigned to Found. Beautiful, huh?Finally, a note on extension methods. Both Where and Select, along with a range of other methods, have been implemented as extension methods. The type they use for "this" is IEnumerable, meaning that any collection that implements that interface can be used with Linq. Without extension methods, it would not have been possible to achieve this level of code re-use.
DLinq and XLinq
In this article I have demonstrated Linq working with objects instantiated from classes that we implemented ourselves and stored in built-in collection classes that implement IEnumerable. However, the query syntax compiles down to calls on extension methods. This means that it is possible to write alternative implementations of Linq that follow the same syntax but perform different operations.Two examples of this, which will ship with C# 3.0, are DLinq and XLinq. DLinq enables the same language integrated query syntax to do queries on databases by translating the Linq into SQL. XLinq enables queries on XML documents.
Conclusion
Linq brings declarative programming to the C# language and will refine and unify the way that we work with objects, databases, XML and whatever anyone else writes the appropriate extension methods for. It builds upon the language features that we have already seen in the previous parts of the series, but hiding some of them away under syntactic sugar. While the query language has a range of differences to SQL, there are enough similarities to make knowledge of SQL useful to those who know it. However, its utility is far beyond providing yet another way to work with databases.

Silverlight


Content :


What is Silverlight?
Silverlight 1.0 :
Main features :
Silverlight 2.0 :
Main features :
How Silverlight would change the Web:
Getting Started With SilverLight :
What is Silverlight?
Silverlight is a new cross-browser, cross-platform implementation of the .NET Framework for building and delivering the next generation of media experiences and Rich Interactive Applications(RIA) for the web. It runs in all popular browsers, including Microsoft Internet Explorer, Mozilla Firefox, Apple Safari, Opera. The plugin required to run Silverlight is very small in size hence gets installed very quickly.
It is combination of different technolgoies into a single development platform that allows you to select tools and the programming language you want to use. Silverlight integrates seamlessly with your existing Javascript and ASP.NET AJAX code to complement functionality which you have already created.
Silverlight aims to compete with Adobe Flash and the presentation components of Ajax. It also competes with Sun Microsystems' JavaFX, which was launched a few days after Silverlight.
Currently there are 2 major versions of Silverlight:
Silverlight 1.0 and Silverlight 2.0( previously referred to as version 1.1).



Silverlight 1.0 :
Silverlight 1.0 consists of the core presentation framework, which is responsible for UI, interactivity and user input, basic UI controls, graphics and animation, media playback, DRM support, and DOM integration.
Main features of Silverlight 1.0 :
Built-in codec support for playing VC-1 and WMV video, and MP3 and WMA audio within a browser.
Silverlight supports the ability to progressively download and play media content from any web-server.
Silverlight also optionally supports built-in media streaming.
Silverlight enables you to create rich UI and animations, and blend vector graphics with HTML to create compelling content experiences.
Silverlight makes it easy to build rich video player interactive experiences.
Silverlight 2.0 :
Silverlight 2.0 includes a version of the .NET Framework, with the full Common Language Runtime as .NET Framework 3.0; so it can execute any .NET language including VB.NET and C# code. Unlike the CLR included with .NET Framework, multiple instances of the CoreCLR included in Silverlight can be hosted in one process. With this, the XAML layout markup file (.xaml file) can be augmented by code-behind code, written in any .NET language, which contains the programming logic.
This version ships with more than 30 UI controls(including TextBox, CheckBox, Slider, ScrollViewer, and Calendar controls), for two-way databinding support, automated layout management (by means of StackPanel, Grid etc) as well as data-manipulation controls, such as DataGrid and ListBox. UI controls are skinnable using a template-based approach.
Main features of Silverlight 2.0 :
A built-in CLR engine that delivers a super high performance execution environment for the browser. Silverlight uses the same core CLR engine that we ship with the full .NET Framework.
Silverlight includes a rich framework library of built-in classes that you can use to develop browser-based applications.
Silverlight includes support for a WPF UI programming model. The Silverlight 1.1 Alpha enables you to program your UI with managed code/event handlers, and supports the ability to define and use encapsulated UI controls.
Silverlight provides a managed HTML DOM API that enables you to program the HTML of a browser using any .NET language.
Silverlight doesn't require ASP.NET to be used on the backend web-server (meaning you could use Silverlight with with PHP on Linux if you wanted to).
Silverlight 2 includes Deep Zoom, a technology derived from Microsoft Live Labs Seadragon. It allows users to zoom into, or out of, an image (or a collage of images), with smooth transitions, using the mouse wheel. The images can scale from 2 or 3 megapixels in resolution into the gigapixel range, but the user need not wait for it to be downloaded entirely; rather, Silverlight downloads only the parts in view, optimized for the zoom level being viewed.
Silverlight 2 also allows limited filesystem access to Silverlight applications. It can use the operating system's native file dialog box to browse to any file (to which the user has access).

How Silverlight would change the Web:
Highest Quality Video Experience : prepare to see some of the best quality videos you have seen in your life, all embedded in highly graphical websites. The same research and technology that was used for VC-1, the codec that powers BluRay and HD DVD, is used by Microsoft today with its streaming media technologies.
Cross-Platform, Cross-Browser : Finally build web applications that work on any browser, and on any operating system. At release, Silverlight will work with Mac as well as Windows! The Mono project has also already promised support for Linux!.
Developers and Graphic Designers can play together! : Developers familiar with Visual Studio, Microsoft.net will be able to develop amazing Silverlight applications very quickly, and they will work on Mac's and Windows. Developers will finally be able to strictly focus on the back end of the application core, while leaving the visuals to the Graphic Design team using the power of XAML.
Cheaper : Silverlight is now the most inexpensive way to stream video files over the internet at the best quality possible. Licensing is dead simple, all you need is IIS in Windows Server, and you’re done.
Support for 3rd Party Languages : Using the power of the new Dynamic Language Runtime, developers will now be able to use Ruby, Python, and EcmaScript! This means a Ruby developer can develop Silverlight applications, and leverage the .net Framework!
Cross-Platform, Cross-Browser Remote Debugging : If you are in the need to debug an application running on a Mac, no problem! You can now set breakpoints, step into/over code, have immediate windows, and all that other good stuff that Visual Studio provides.
The best development environment on the planet : Visual Studio is an award winning development platform! As it continues to constantly evolve, so will Silverlight!
Silverlight offers copy protection : Have you noticed how easy it is to download YouTube videos to your computer, and save them for later viewing ? Silverlight will finally have the features enabling content providers complete control over their rich media content! Streaming television, new indie broadcast stations, all will now be possible!
Extreme Speed :There is a dramatic improvement in speed for AJAX-enabled websites that begin to use Silverlight, leveraging the Microsoft .net framework.


Getting Started With SilverLight:
In order to create Silverlight applications with following :
Runtime :
Microsoft Silverlight 2.0 : The runtime required to view Silverlight applications created with .NET Microsoft.
Developer Tools :
Microsoft Visual Studio 8.0 : The next generation development tool.
Microsoft Silverlight Tools for Visual Studio 2008 : The add-on to create Silverlight applications with Visual Studio 2008. This install will also install the Silverlight Developer Runtime and the Silverlight SDK. This add-on works with all versions of Visual Studio 2008 Service Pack 1, including Visual Web Developer.
Designer Tools :
Download the Expression designer tools to start designing Silverlight application.
Expression Blend 2.5The latest offering from Microsoft to create Silverlight content is
Expression Blend 2.5 June 2008 Preview.
Software Development Kit:
Microsoft Silverlight Software Development Kit: Download this SDK to create Silverlight Web experiences that target Silverlight 2.0. The SDK contains documentation, tools, Silverlight ASP.NET controls and the libraries needed to build Silverlight applications.
Eclipse Tools for Silverlight : An Open Source, feature-rich and professional RIA application development environment for Microsoft Silverlight in Eclipse.