Converting C# Anonymous Functions to SQL Where Clauses using Expression Trees

C# Anonymous functions are awesome, they allow you to abstract your code in a beautiful and clean synax.
But trying to integrate this with reflection and generics might get a little bit hard, I had a problem a few days ago... Generate a SQL WHERE clause based on a function, which takes an object of ta generic ype T, and returns a boolean (f(x: T): Boolean).

The function itself will be pretty simple most of the time, the concrete example of the method I was trying to accomplish is the following: db.Update<MyType>(myObj, (o => o.Id == 1))

So basically, that will update the conveniently named table MyType, with myObj data, WHERE Id = 1

So how take an anonymouse function, parse it, and return a string? I knew it was possible, because LINQ does this for SQL, so there must be a way. The first thing I did was search on StackOverflow, which lead me to this good article on Expression Trees.

After learning a bit about that, I knew I had to use Expression Trees, but I still didn't quite understand how, I wanted to use a function on my method, not a Expression Tree, and there is no way to get an Expression Tree from a function, so I asked on StackOverflow, the answer was pretty simple, just use a function on your method call, and in your Update method use a ExpressionTree as argument type.

public bool Update<T>(T obj, Expression<Func<T, bool>> predicate)

That was I can call my function like this Update<MyType>(obj, (t => t.Id == 1))

That was a huge step, now I could work on the Expression Tree itself, I ended up with a quite nice function to get a string based on a Expression Tree, it assumes the body it's just a list of conditions, for example A && (B || C), which is what you'll most likely have to cope with.

Here is the code I used to get a string from my expression, also note the GetValue method is a helper method, used to get the value of a MemberExpression (Thanks again, StackOverflow!)


plog

Plog stands for Pragmatic Blog, it's a name derived from PPLOG, one of my old projects, it's no longer mantained, but the idea was a simplistic blog, PPLOG is just one big perl file, you drop on your webserver, and it just works.

Plog, on the other hand, is oriented at developers, and runs on the desktop, it generate a bunch of static HTML files which you can then upload to any webserver, no server configuration, no hassle, just plain HTML files.

Plog is still on development, it's on .Net, but it can also be run on Linux using Mono. The idea is to set up a configuration file, conveniently named config.json, and just run the plog command on the console to generate static HTML files based on your configuration and layout.

The workflow could be something like this

  • Create a folder named Blog
  • Create a folder named Entries inside Blog
  • Create a Markdown file in the Entries folder, with the name "yyyy-mm-dd Entry Title.md", for example "2012-10-02 My First Entry.md"
  • Open a terminal / console and navigate to the Blog folder
  • Run the plog command
That will generate HTML files in /Blog/Deploy, based on your configuration and layout. Whenever you add or modify a file, just run the plog command again to re-generate all the files.

More information about the usage will be added on future posts, but once you have set up your environment, all you'll have to do is create a  .md file, and run the plog command, if you use git you can use github to host your blog, and update your blog using your favourite editor and the terminal.

Plog will also be able to upload the generated file using FTP if specified in the configuration.