Random thoughts of wisdom - the personal blog of Michael Holm Andersen

LINQ and PagedList

If you’re using LINQ you probably know about the .Skip and .Take methods. However often you need to do paging, hence you probably use something similar to this to get around that:

public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        PageIndex = pageIndex;
        PageSize = pageSize;
        TotalCount = source.Count();
        TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

        this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));

However you don’t need the above code – instead take a look at the CodePlex project called PagedList located at

PagedList allows you to take any List<T> and by specifying the page size and desired page index, select only a subset of that list, there’s also a StaticPagedList if you should need that.

Simply download the source, run “Release.bat” to build a release version and reference the .dll file in your solution, then you can have code like this:

var somePage = list.ToPagedList(4, 50); // fifth page, page size = 50

Using Fiddler with the ASP.NET Development server

Fiddler does not by default work together with the ASP.NET development server, however you can easily fix this.

In Fiddler press Ctrl+R (Rules) and go to the “OnBeforeRequest” method, here you add a single line: ="mhanotebook","");

The “mhanotebook” is the name of your computer. The problem is that Fiddler works as a proxy and change this settings in IE7 to forward request to fiddler. however IE7 does not forward calls if the url contains localhost.

After applying the above rule, you can now do a request like this: http://mhanotebook:50016/product and Fiddler will “kick in”, nice :-)

Using LINQPad to query the database

If you’re working with LINQ, you simply can’t live without LINQPad. Below is a screenshot from the query:

from ans in UserAnswers
group ans by new {ans.AnswerId, ans.Answer.Content, 
into myGroup
select new {myGroup.Key.Q, myGroup.Key.Content, Antal = myGroup.Count()}

- being able to do realtime queries against the database is simply awesome. And for only $29 you’ll have Autocompletion (which of course is a must-have / timesaver).

The query shows simple statistics about a poll, where question (Q), answer (Content) and total votes (Antal) is pulled out from the database – I’ve blurred the value of Count() in order to ��protect” the clients data…


LINQ Projection and SelectListItem

A small example of where LINQ Projection might come in handy is when you’re working with MVC. In the below example LINQ Projection is used to transform a enumeration of EmployeeCategory instances into an enumeration of SelectListItem instances:

var rep = new mhaRepository();
var person = rep.GetPersonByID(id);
ViewData["EmployeeCategories"] = from c in rep.EmployeeCategories
                         select new SelectListItem
                             Text = c.Name,
                             Value = c.ID.ToString(),
                             Selected = (c.ID == person.CategoryID)

In the View all that is needed to render the DropDownList is this:

<%= Html.DropDownList("EmployeeCategories") %>

Sidebar translation gadget

I’ve been looking for a translation gadget and today I (finally) found a good one:


It uses Google as the “engine “, supports 43 languages and is free:

Canon EOS 350D and Windows 7 x64

Having been out a couple of hours today in the snow with my Canon EOS 350D, I discovered that Windows 7 x64 was unable to detect the camera. After some research I found out that if I changed the camera's USB connection mode from "PC" to "Print/PTP", Windows detected the camera without any problems and I had no problem whatsoever copying my images over.

A few of the images…

IMG_3847IMG_3880IMG_3882 IMG_3931 IMG_3946

Cheap SSD from Kingston

I’ve just ordered a (cheap) 64GB SSD from Kingston for my HP Notebook which was “born” with the factory 5400RPM drive and it’s sloooooooow – however it’s nice to see that prices of SSD’s are finally dropping to a level where it’s affordable to say “bye bye” to those awfully slow HDs that most notebooks ship with.

You can find more info here (in danish)

Gradient typography in webdesign

Just surfin’ the web, I found this nice blog post about gradient typography in webdesign and should I would share it :-)