Posts from  November 2009


Remove<T>(this IList<T> source, Func<T,bool> selector) why not?

Maybe I am just crazy, but it seems like removing or deleting items from a collection is always an after thought. Take IList for example, a list of items, with the ability to add and remove from it. We have a flurry of extension methods that are inherited from IEnumerable to add items but it seems like no one thought maybe it would be nice to beef up the remove method with some extension methods. Maybe I missed an extension namespace, maybe I am just crazy. How many times do we have to write the following bloated code just to remove something from a collection?

var ingredient = Ingredients.FirstOrDefault(i => i.FeedId == feed.Id);

Even worse, is when we want to remove multiple items from a list based on some criteria. I have to add a foreach loop of some sort to remove each item one at a time. Oh and, deal with the always wonderful: (System.InvalidOperationException: Collection was modified; enumeration operation may not execute). So, I have to remember to create a new list to avoid this dreadful mistake:

var ingredients = Ingredients.Where(i => i.FeedId == feed.Id).ToList();
foreach (var ingredient in Ingredients)

Of course, now that I have created a new list, ToList(), I can simplify this to:

var ingredients = Ingredients.Where(i => i.FeedId == feed.Id).ToList();
ingredients.ForEach(i=> Ingredients.Remove(i));

But that is still icky, all that code just to remove an item or a set of items? So enough complaining on my part, it's time to put up or shut up. This is what I want to do in code:

Ingredients.Remove(i => i.FeedId == feed.Id);

Simple, right? And to do this here is the simple extension method:

public static void Remove<T>(this IList<T> source, Func<T, bool> selector)
  if (source == null || selector == null)

var itemsToRemove = source.Where(selector).ToList(); itemsToRemove.ForEach(i => source.Remove(i)); }

Now, I no longer have to worry about all these things when I want to remove items based on searching my list:

  • Doing a separate search for the items I want to remove
  • Creating a new list, avoiding the pitfall of enumerating when removing and getting "Collection was modified" exceptions.
  • Iterating over those items and calling remove on each

Some purists out there would be mad that I just quietly allow the source/selector to be null and not throw an exception. That's how I chose to implement this, if you want different behavior that's great. Just make sure you add this extension method to your arsenal! Maybe another RemoveSingle that throws an exception if it finds 0 or more than 1 item would be appropriate to add as well?

Happy coding!


SyntaxHighlighter with Subtext/GWB

I've been very frustrated with the lack of flexibility with hosted blogs, so I am yet again moving. This time I wanted to host my own instance of Subtext but discovered that uses Subtext and figured I would give one last attempt to a hosted community online :) I guess I just don't want to deal with running my own server just for a blog.

The most important thing I wanted back was Syntaxhighlighter which was about impossible to use with without paying or running my own instance. I figured I would share how other people can use Syntaxhighlighter with Subtext.

What is it? It is a great tool to bring your code samples to life with very little trouble. All you have to do is mark them with <pre> tags and it does the rest. It supports pretty much any language you want to use and can be extended for others.

Steps to integrate with Subtext/GWB

  1. Pick a theme, I prefer the RDark theme.
  2. Modify Custom CSS under Options->Configure
    1. Add these css imports.

      @import url(""); @import url("");
    2. Note: If you want a theme besides RDark, set that instead.
    3. Note: I have this setup to use the current version of SyntaxHighlighter, if you need an old version replace current in the url with the version.
  3. Modify Static News/Announcement in Options->Configure, if you have a SubText install you are hosting and you have the option for Sitewide Tracking code, you can use that as well.

    1. This is where you can insert the javascript needed to perform the highlighting.
    2. Include the shCore.js

         <script type="text/javascript" src=""></script&gt; 
    3. Include any brushes, I have c#,python,javascript and css included here:

          <script type="text/javascript" language="javascript"    src=""></script&gt;
          <script type="text/javascript" language="javascript"    src=""></script&gt;
          <script type="text/javascript" language="javascript"    src=""></script&gt;
          <script type="text/javascript" language="javascript"    src=""></script&gt;
    4. Enable highlighting

  4. See this link for how to use it.
    1. Huge Fail: FCKEditor, at least the version used here on GWB puts <br> tags in <pre> tags, stupid idiots, and it strips formatting so until this is fixed this will be a major pain, in the mean time I am using Windows Live Writer and I have the HTML formatting option set (turn off XHTML formatting or it will reformat pres).