I had to use the Javascript encode method to escape some data in a hidden control recently. That data was then posted and I wanted a method to be able to convert it back. It turns out that the way to do it is using Uri.UnescapeDataString.

If anyone knows of a better method, or any issues with this approach, please leave a comment.

jQuery Ancestor Plugin

September 8, 2009

Today I wanted to select a parent element somewhere up the hierarchy using jQuery. I’ve since found that you can use the Parents function to do a similar job. In my case however I wanted the next parent item that matched the specified selector (I’m really only looking for a single item). I decided to create a jQuery plugin to do the donkey work for me.

Here is the implementation:

jQuery.fn.ancestor = function(selector) {
  return check($(this), selector);
}

function check(element, selector) {
  if(element.is(selector)) {
    return element;
  } else if(element.is("body")) {
    return null;
  } else {
   return check(element.parent(), selector);
  }
}

And here is an example usage:

$(this).ancestor(".menu-row");

I think this might be useful to anyone who wants to create their own jQuery plugins, and perhaps the actual plugin itself will be useful. Feel free to grab it.

Open Source CMS

July 15, 2009

Started work on Aardvark, an open source Content Management System, to be written in .NET.

Just had the need to compare that a list was in the correct order for a test I’d written. Handily there is a Linq extension method that makes the job easy, as the following snippet shows.

private static bool 
 OrdersMatch(IEnumerable newsItems, IEnumerable newsIds)
{
  return newsItems.Select(n => n.Id).SequenceEqual(newsIds);
}

You can use an extension method as I have, if you need to match on a specific part of the objects you are comparing.

This is something I have to do about two times a year and always forget. It’s useful if you are restoring a backup from a development or live server, and the data files are pointing in different places.

RESTORE DATABASE SOME_DB
FROM DISK = 'C:\backups\some_db.bak'
WITH REPLACE,
	MOVE 'some_db' TO 'C:\Program Files\Microsoft SQL 
              Server\MSSQL.1\MSSQL\Data\dsome_db.mdf',
	MOVE 'some_db_log' TO 'C:\Program Files\Microsoft SQL
              Server\MSSQL.1\MSSQL\Data\some_db_log.ldf'

LINQ Sytax

June 17, 2009

This post was going to be longer but I decided to cut it down, mainly due to the fact that it has been sat in my drafts for about 6 months. So in the end it’s a (very) short post about different LINQ to entity syntax, nothing to taxing but someone might find it useful.

As you may be aware LINQ provides various extension methods that allow you to perform query like operations (the ones we are interested in right now are Select, SelectMany and Where). This allows you to get data from any IEnumerable collection.

There are two syntaxes that you can use to get data from a collection.

The first is SQL like:

   1: var pilots = from tieFigher in tieFighters select tieFigher.Pilot;

Or using extension methods:

   1: pilots = tieFighters.Select(n => n.Pilot);

The second method uses the Select method which takes a Lamda Expression that allows us to specify what we want to select; in this case the Pilot. Both of these return the same result, and now that we have a list of pilot strings, we can iterate through them using foreach:

   1: foreach (var pilot in pilots)
   2: {
   3:   Console.WriteLine(pilot);
   4: }

So now we understand how to Select something but what would be really great, is if we could select a particular something. To do this is where we have to make use of the Where clause, again I will demonstrate both syntaxes.

The SQL like syntax:

   1: var pilots = from tieFighter in tieFighters
   2:     where tieFighter.Pilot == "Darth Maul"
   3:     select tieFighter;

And the equivalent would be:

   1: pilots = tieFighters.Where(n => n.Pilot == "Darth Vader");

As pointed out at the start this post has been dramtically cut down, I aim to publish something I bit more detailed soon.

SocialSafe

June 17, 2009

The company I work for, have just co-launched a great little tool for backing up your Facebook details. It’s called SocialSafe, and you can get it here.