Data for the application was pulled out of a Linq to SQL class, working off a SQL database. (I don’t want to get into a discussion on the semantics of my design choice – perhaps Items should be members of Categories instead of the other way around. Just enjoy the show for now…)

What I wanted to do was to filter a Linq query from the database (containing IQueryable<Item>) for items that contained Categories with a given CategoryID. No problems! Just run up a Linq to SQL query to get the Category instance matching that CategoryID as follows:

Note that the filter as shown below won’t work on an IQueryable<Item> (due to the Contains() clause), so we’ll evaluate the incoming IQueryable<Item> as it comes in, and (for convenience) convert it back to IQueryable on the way out. We’ll implement the method as an extension method, because we can.

It may seem obvious, but the reason the second Assert fails is that, despite having the same internal value, a and b are not the same! And this is the problem with my filter query above. Despite the Category instance I received from the SQL query having the same internal values as (perhaps) a Category in the Categories property of an Item, they are not the same object and so the comparison will fail.
What we need is some way of telling .NET that two instances of an object are the same, even if they are different objects, and this is where the IEqualityComparer comes in. We can create the following class:

We have created a class that says that two instances of Category are equal when their CategoryID values match, and have based their Hash Code on the CategoryID value rather than the instance itself. All we need to do now is to plug it in to the query, as follows:

I am writing a WPF application which runs as a tool-style dialog on the desktop, and needs to be able to be minimized but not maximised. It seems that this is somthing the WPF designers didn’t quite anticipate, despite it having been pretty easy to achieve under Windows Forms.

After searching around the ‘net, I finally found some code that disabled both the minimize & maximize buttons, but took a couple shortcuts that I didn’t like (and besides, it was in VB…).

A bit of searching and a quick cleanup left me with a working result. Catch the Loaded event of your window, and put the following code into the handler:

For the last couple of years, I’ve been writing a program to decode DVB transport streams (more info at the Multiflex site), and have recently started implementing the decoding of H.264 (MPEG-4) video streams.

One challenge I have come across is to find the last instance of a certain class of object within an array of (generic) objects, for example to find the last instance of a class of type ‘Foo’ within ‘object[] args’.

I learnt C# 1.0 back in the early part of this millenium, then changed jobs and concentrated on C++ and ANSI C (yes, I even bought a copy of Kernighan & Ritchie). When I switched back to C# a couple of years ago, I basically had to re-teach myself all that I knew, and since C# 2.0 was in vogue then, that’s what I learnt.

Now, while I pride myself on being an early adopter in most areas, I have managed to successfully ignore C# 3.0 until now. Honestly, who really needs LINQ anyway?

Oops!

I found some very informative talks from TechEd on MSDN’s Spotlight site recently which do a great job of highlighting the changes in C# 3.0 and introduce LINQ and Lamda Expressions in a really easy-to-digest format. So easy that even I was forced to think, “Crap! Why didn’t I get into this stuff sooner?”