Linq operators on null Linq collection

September 25, 2013

You cannot apply the Linq operators (First, Single, Any, …) when the query returns null.

We have to add an extension method:

public static IEnumerable<TSource> NullToEmpty<TSource>(
    this IEnumerable<TSource> source)
    if (source == null)
        return Enumerable.Empty<TSource>();

    return source;

And now you can write:

public void Should_handle_nulls()
    List<int> ints = null;


LINQ parameter == null with SQL CE

August 6, 2013

SQL CE cannot handle parameters so, the so standard construction

.Where(p => p.Category == category || category == null)

fails because LINQ needs to use a parameter to do that value comparison in-query.  But it is perfectly valid SQL to pass something like this:

.Where(p => p.Category == category || true)

because ‘true’ is always…well…true.  So if you parse your variable to a bool before the LINQ statement and then pass that variable to SQL CE, you’re in good shape:

bool bCategory = (string.IsNullOrEmpty(category)) ? true : false;
Products = repository.Products.Where(p => p.Category == category || bCategory);

This allows you do to all the filtering in the query before returning data, and you can use this trick on as many conditions as you want without it getting messy.

Office Development

July 4, 2013

Office development libraries:

Interesting are the articles about the Open XML format SDKs


CSS3 Overview + Navigation menus

January 18, 2013

All info about css3:

A cool navigation menu generator

Images for GUI design

December 19, 2012

Visual Studio Images

for MSVC 2005 in C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary

Windows Images



Stock Photo Companies

Stock photos are a great way to get more generic photos for use on your Web pages. They provide photos of people, products, places, and animals and are well lit and shot. And while most stock photo companies are not free, there are a few free ones and there are also some that provide high quality photos for a low price. And remember, since you’re buying photos for a Web page, you don’t need to pay for resolutions that would print well. This usually lowers the price. Some stock photo companies include:

Public Images

Finally, you can use public images on your website. Most photos taken by the government can be freely used. Be sure to check the copyright before you use them. Some public domain image sites include:

IOC Container

October 19, 2012

Server Tags

September 24, 2012

Server Tags:

<% %> An embedded code block is server code that executes during the page’s render phase. The code in the block can execute programming statements and call functions in the current page class.

<%= %> most useful for displaying single pieces of information.

<%# %> Data Binding Expression Syntax.

<%$ %> ASP.NET Expression.

<%@ %> Directive Syntax.

<%– –%> Server-Side Comments.

<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4).

Visual Studio Editor Reformat code

August 24, 2012

Realign code:

Ctrl K+D will format the entire document.
Ctrl K+F will format the selection.

Microsoft Practices EnterpriseLibrary Logging Timestamp

August 9, 2012

It displays the UTC time. To display the time on your machine, use the token {timestamp(local)} instead of {timestamp} in your formatter.

CSS selectors

June 8, 2012

CSS selectors for CSS1, CSS2 & CSS3