Jesper Niedermann's .NET Blog
about .NET and related technologies RSS 2.0
# Wednesday, 18 March 2009

What is Linq ? You probably think you know the answer, Linq is after all already quite dated. Well in my experience chances are you only know part of the story. Most of the people I have met have a slight misconception of Linq which limits their use of this great technology.

Let me explain. Linq of course means "Language Integrated Query", we all knew that. This basically refers to the syntactic sugar that Microsoft has poured over the C#3.0 language. I.e the from, where and select keywords and a few more.

In practice Linq is more. It is also a set of Extension methods in a given Linq provider, and a new way of coding, and furthermore many of the C#3.0 features are there to make Linq more useful. Like the var keyword, and lambda expressions.

What I think is limiting the use of Linq is that most people think Linq is a database technology. It is true that Linq can be used for querying databases if you use a Linq provider for a database technology like The LinqToEntities, LinqToSQL or Linq to some other ORM. This is basically Microsoft's "fault", Linq can be used for querying databases, and that is how Linq was promoted, I guess because of the ORM hype.

I was so fortunate to talk to one of the creators of Linq, Anders Hejlsberg in October 2008 when he was giving a lecture in Hilleroed, Denmark. I presented my view to him, that I thought it was a pity that Linq was viewed only as a database technology. He agreed with me, and said that this was because database access was the driving force behind Linq to begin with, and for marketing reasons.

But what is Linq really then ? If you look at an old fashioned SQL query

SELECT SomeColumn FROM SomeTable

You query relational data (SomeTable), and in return you get relational data (A set containing SomeColumn). With Linq you have much more power. You can query anything that implements the IEnumerable<XXX> interface, and you can return one or more .NET objects of any type.

So therefore I think it is much more productive to look at Linq as a means to transform data. In some ways Linq can replace XSLT, I am no XSLT expert but I would guess that you can always write a Linq expression to replace any XSLT transformation, as long as you work in a .NET assembly. Of course an XML document can refer directly to an XSLT transformation, which is not possible with Linq.

Transformations of data are so common in a computer program that we don't even notice them. In fact we use them all the time. And consequently we can use Linq expressions all the time, and should use them most of the time. I would argue that a Linq expression is more readable than "normal" code most of the time. If it is less readable or performs badly you might choose another option.

Here are a few examples of transformations that we don't think of as transformations:

  • Outputting errors to a logfile, or the console, or an eventlog. In this case exceptions are transformed to lines of text.
  • Building a dynamic menu from data in an XML file. In this case the data in the XML file is transformed into menuitems.
  • Building a dynamic Xaml page from some input. The input data is transformed into Xaml.

I will know give you some concrete codefragments to show these types of queries.

Outputting errors

Let's say that you have collected error information in the form of a List of Exceptions. Assume in the following code that ErrorList is a List<Exception>:

(from excep in ErrorList 
where excep is ArgumentException
select excep).ToList()
.ForEach(e => Console.WriteLine(e.Message));

Now you have transformed your Exception data to Visual Data in the Console. In this example only ArgumentException's are printed. Of course you could easily print something better than just the message, e.g ToString() or perhaps a PrettyPrint() extension method for Exception that digs all InnerExceptions out, and prints everything in a nice readable format.

Building a dynamic menu

This example is from my WiiCursor project:

m_ConfigurationMenu = new MenuItem("Configure");
var confMenus =
from configuration in configurations
select Util.GetMenuItem(
(sender, args) => Configure((MenuItem)sender, configuration),

In the code shown 'configurations' is a List<WCConfiguration> where WCConfiguration is a class that corresponds to an XML file, and gets initialized by the file. The GetMenuItem(...) method basically "news up" the MenuItem and connects a click eventhandler to it by passing the lambda expression '(sender, args) => Configure((MenuItem)sender, configuration)' to the MenuItems constructor.

So when the user clicks the MenuItem what happens is that the Configure(...) method is called with the MenuItem and most importantly the correct WCConfiguration as parameters.

This might not be the most readable code in the universe. In fact I violate one of my own best practices. Only use the var keyword if the resulting type, here MenuItem, is readable in another way. In the original version I called new MenuItem(...) directly, so that is my (admittedly bad) excuse.

But still the code beautifully illustrates how a Linq expression can Transform the context of XML files to MenuItems that act upon the information in the corresponding file.

Building a dynamic Xaml page

In another project of mine, not yet published, I experiment with logging mouse moves to help me register the time I have used on different projects at the end of the week. A common task for employees around the world. I log the mousemoves in an XML file using a custom format.

<?xml version="1.0" encoding="utf-8"?>
<Activity Name="Activity" Start="10-03-2009 08:01:42" End="10-03-2009 10:01:54" />
<Activity Name="Activity" Start="10-03-2009 12:00:00" End="10-03-2009 16:01:05" />

To make it more appealing visually I transform it to a Xaml file that I load dynamically in a WPF project. The smart thing is that I can do it in a VB.NET assembly combining XML Literals and LinqToXML. Below is the first few lines of the Transformer class that contains the methods to create the Xaml file. Notice how the Xaml can be inlined easily in VB.NET since it is also XML.

Imports System.Linq
Imports System.Linq.Enumerable
Imports System.Xml.Linq
Imports <xmlns="">
Imports <xmlns:x="">

Public Class Transformer

    Private Const Column0Width As Integer = 50
    Private Const Column1Width As Integer = 50
    Private Const Column2Width As Integer = 70
    Private Const Column3Width As Integer = 170
    Private Const Column4Width As Integer = 40

    'Converts from a basic XDocument with Activities to a rich one using Xaml.
    'This is done with VB.NET because of the rich LinqToXML experience in VB.
    Public Shared Function ConvertToXaml(ByVal convertFrom As XDocument) As XDocument

        Dim months As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

        Dim doc As XDocument = New XDocument( _
<Window xmlns=""
    Title="Time Statistics" Height="302" Width="514">
        <TabControl Name="ActivitiesTabControl">
            <%= From month In months _
                Select _
                <TabItem Header=<%= NameOfMonth(month) %> Height="20" Width="80" Selector.IsSelected=<%= SelectMonth(month) %>>
                    <ScrollViewer VerticalScrollBarVisibility="Auto">
                            <%= ColumnDefinitions() %>
                            <%= RowDefinitions(NumberOfRows(month, convertFrom)) %>
                            <%= RowHeaders() %>
                            <%= RowsOfMonth(month, convertFrom) %>
                </TabItem> %>
</Window> _
        Return doc
    End Function

XML Literals is the only reason why I sometimes use VB.NET, otherwise I am a C# guy. I would have loved C# to have this nice feature too, my guess is that it was impossible because angel brackets '<' are reserved for generics in C#.

Apart from giving an appealing visual look the code also makes calculations like summing up the time used in an entire day. Linq is ideal for this too.

Summing up

I hope I have convinced you that Linq is more than just a database technology. You knew it already of course, but did you live it and breathe it :O) In my opinion Linq is such an integral part of C#3.0 that there is no avoiding using it all the time.

Wednesday, 18 March 2009 21:49:20 (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Linq
# Friday, 13 March 2009

Any windows user is familiar with Windows Explorer but were you aware that one can access an ftp site or WebDAV enabled site using the Windows Explorer ? I myself haven't really been looking in to this, since I have been using Filezilla to upload files to my website, but why not use Windows Explorer ? It is always at hand with the Windows+E keystroke.

As an example I will show you have to access the site via the Windows Explorer since it is a WebDAV enabled site with anonymous access. The site contains all the famous tools from Sysinternals a company which was purchased by Microsoft.

In order to connect you just have to click on the good old "Map Network Drive ..."


Since I know the name of the subdirectory 'tools', the easiest way seems to be to map to a drive letter directly by typing the name \\\tools in the folder textbox:


If you are trying to connect to an ftp server the right way seems to be to click on "sign up for online storage..." After pressing "Next" twice you get this screen:


Here you can write the name of the ftp server or website if using WebDAV, so here I could have written ''. The next screen makes you enter username/password or Anonymous access. Pretty self explanatory.

The end result is that the site appears under 'My Network Places.'

After adding the sysinternals site to your explorer you get all the usual advantages, easy copying and pasting etc.

BTW there are still reasons for using a "proper" ftp client like Filezilla, e.g. you can specify an alternative port number, which doesn't seem to be an option in the Windows Explorer.

Friday, 13 March 2009 20:58:42 (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Tips & Tricks
# Wednesday, 11 March 2009

Currently I am looking into taking over a huge enterprise legacy system written mostly in C#, but also some C++. The numbers are staggering several million lines of code, 1200 GUI screen, mostly WinForms, about 750 C# projects, 150 solution files, just to mention a few. In all about 4 times larger than the largest enterprise system I have previously been working on.

Just building and deploying the system is a pretty big task since due to events beyond my control I basicly only have the sourcecode and nothing else. Not quite true because there are some documentation, but buried in the thousands of files.

When I managed to modify a build file and build the entire monster with it, it took 2½ hours to complete.

I won't even try to include all projects in a single Visual Studio file. I wonder if it would load ? This gives me a problem though. How about debugging ? I cannot press F5 for any of the visual studio projects because the build process using the build file set up a ton of config files with IP adresses, connection strings and the like. So it doesn't make sense to run the thing from Visual Studio.

But if I start up the exe assembly how do I attach a debugger ? Well, I decided to use a simple trick. I added a line of code in the top of the main() method:

static void Main(string[] args) 

Debugger.Break() is basicly a programmatic breakpoint, but it is more than that. If no debugger is attached it asks the user for a debugger. Smart! And for the process to debug I can just choose the Visual Studio process with the project containing the exe assembly. Henceforth the control is mine.

I could even consider writing this method and using it instead:

private static void DebuggerBreak()

This gives me the advantage that the IL code is only build in DEBUG mode and that the Debugger is only attached if one is not attached already.

Wednesday, 11 March 2009 06:23:39 (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tips & Tricks
# Sunday, 08 March 2009

When I post code blocks in this blog. It would be nice if they look like, well, code. So I looked at what Scott Hanselman had done, he is after all one of the creators of the blogengine I use: "Dasblog"

In this post Scott explains how to do it. He explains how to add Javascript from a project called SyntaxHighlighter and use a plugin called precode for Windows Live Writer. Well I use WLW too, so this is perfect for me.

But I had a few problems. I took me some time to figure out that the newest edition of SyntaxHighlighter didn't work with precode.

So I have now settled on SyntaxHighlighter version 1.5.1 and precode version 3.0.0

SyntaxHighlighter is nice because it has support for multiple languages, and it is easy to add support for more. The caveat is that it hasn't got any knowledge of the language. Which means that it just parses the text and looks for keywords. So stuff that looks like keywords will be color highlighted too. I consider this a minor problem.

I chose to add the Javascript in the scripts folder of my blog, and the SyntaxHighlighter.css file in template folder.

Know I could easily add the required code in the bottom of my homeTemplate.blogTemplate file, immediately before the closing <body> tag, like so:

<script class="javascript" src="scripts/shCore.js"></script>
<script class="javascript" src="scripts/shBrushCpp.js"></script>
<script class="javascript" src="scripts/shBrushCSharp.js"></script>
<script class="javascript" src="scripts/shBrushJScript.js"></script>
<script class="javascript" src="scripts/shBrushSql.js"></script>
<script class="javascript" src="scripts/shBrushXml.js"></script>
<script class="javascript">
dp.SyntaxHighlighter.ClipboardSwf = 'scripts/clipboard.swf';

Of course if you don't use DasBlog you just put the code in the bottom of each page.

Furthermore you need to refer to the stylesheet in the top of each page.

The last thing I did was to modify the stylesheet to make the color scheme look a bit more like visual studio.

And now I can post code like this nice c# one-liner that shows the name of every file in the current directory:

using System;
using System.IO;
using System.Linq;

namespace ListNames
class Program
static void Main(string[] args)
(from file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories)
select file).ToList().ForEach(file => Console.WriteLine(file));

A coworker of mine showed me a program that could do this, and I said well I can do that in one line - it is always nice to show off :) - (disclaimer:  I do not generally advocate to code in as few lines as possible. :) One issue you must always consider is readibility, though generally I must say that one of the great advantages of C#3.0/Linq syntax is that it tends to make stuff more readable when used correctly.

Speaking of Linq, as you can see the Syntaxhighlighter doesn't recognize Linq syntax. The "from" and "select" keywords are not highlighted (unless you read this when I have updated the shBrushCsharp.js script).

Sunday, 08 March 2009 12:07:11 (GMT Standard Time, UTC+00:00)  #    Comments [2] -
.NET | blog
# Tuesday, 24 February 2009

This is the first post of my new blog. Hope you will like it. My plan is to blog about my favourite topic - .NET and other more or less related technologies. I hope to post regularly but I will keep my ambitions low and plan only to post once every month ! Perhaps with a larger more in-depth blog post every second month. This is not for lack of subjects, but because I know myself. Work, family and fitness training often comes before late night programming and researching new stuff.


Though my mother-tongue is danish I plan to keep this blog in English to reach a wider audience.


Take care…

Tuesday, 24 February 2009 22:39:57 (GMT Standard Time, UTC+00:00)  #    Comments [2] -
<2009 March>
About the author/Disclaimer
I am a software architect with focus on Microsoft Technologies. I have been working with these in different large companies since 1995. I am currently employed at UVdata A/S.
Here is my View Jesper Niedermann's profile on LinkedIn

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Privacy policy
The privacy policy of this site.

© Copyright 2018
Jesper Niedermann
Sign In
Total Posts: 28
This Year: 0
This Month: 0
This Week: 0
Comments: 32
All Content © 2018, Jesper Niedermann
DasBlog theme 'Niedermann' created by Jesper Niedermann, based on 'Business' created by Christoph De Baene (delarou)