LINQ Distinct without IEqualityComparer

by Richard Mason 17. August 2009 17:11

One annoying issue with the LINQ Distinct function is that it requires an IEqualityComparer. Often I'd (and many others based on the info out there) would prefer to just provide a lamdba indicating which field to compare on, or simply a predicate for doing the compare.

There's at least one solution out there which uses a generic implementation of IEqualityComparer which works fine and another which simply implements the Distinct iteratively. Instead I figured you could instead build it up using Reduce... woops I mean Aggregate.

static IEnumerable<T> Distinct<T>(this IEnumerable<T> list, Func<T, T, bool> comparer)
{
  return list.Aggregate((IEnumerable<T>)new T[0],
    (a, b) => a.Any(c => comparer(c, b)) ? a : a.Concat(new[] {b}),
    a => a);
}
 
static IEnumerable<T> Distinct<T>(IEnumerable<T> list, Func<T, object> selector)
{
	return Distinct(list, (a, b) => selector(a).Equals(selector(b)));

 

Two versions, one for just specifying that the compare should happen on some calculated value and another for providing a custom compare function. 

Now this might not be the most efficient method of implementing this, but it's kind of nice, but I'm a bit unhappy with the fact that this is no longer a lazy list. I have a solution for that as well, but it's nowhere near as clean as this :(

TimeSpanPicker released under BSD

by Richard Mason 30. April 2008 17:49

A request via email has encouraged me to release the TimeSpanPicker control I blogged about a few years ago. The details are available at http://www.rikware.com/page/TimeSpanPicker.aspx but I've copied them in here for ease of reading: 

The TimeSpanPicker is a Windows Forms UpDown control for editing TimeSpan values. It is currently used in RikPVR (http://www.rikpvr.com/)

The control is provided, as is, under a BSD license (meaning it's free to use and alter in free or commercial projects). Any improvements will be greatly appreciated.

A Visual Studio 2008 solution (including binaries) containing the project is available here: TimeSpanPicker.zip (11.43 kb)

The project targets .NET 2.0 and should be simple to recompile in Visual Studio 2005.

The control supports input via keyboard, mouse wheel or via the up/down buttons. It is also theme aware.

kick it on DotNetKicks.com

RikMigrations - There was movement at the station

by Richard Mason 28. March 2008 06:17

RikMigrations has started rolling again recently. It appears some posts on the alt.net mailing list prompted a few people to check it out and even better some have signed on to contribute as well. This prompted me to commit the changes I've made in the last few months (I'd been meaning to do it but had been lazy).

There's already the start of an msbuild task for RikMigrations and rumblings of a NANT one as well. Plus, a nice little tweak to use generics to define column types so you can do this:

t.AddColumn<int>("ID"); 

And there's also discussion starting around adding some ability to populate the database with initial values in migrations as well (https://www.codeplex.com/Thread/View.aspx?ProjectName=RikMigrations&ThreadId=24868)

PS. For the non-Australians out there the title refers to a famous Australian poem.

kick it on DotNetKicks.com

Announcing RikMigrations

by Richard Mason 22. November 2007 04:45

Over the last couple of weeks I've been working on a migrations framework for .NET called RikMigrations. There's a couple of existing frameworks but they're mostly integrated in other large projects like SubSonic or didn't quite fit my needs/wants.

RikMigrations is currently relatively small and simple, but I am releasing it under a BSD license on CodePlex so others can jump on board and build it up. Check out the project homepage at http://www.rikware.com/RikMigrations.html to find out more, including a simple tutorial demonstrating its use. 

kick it on DotNetKicks.com

WiX v3 Roadmap

by admin 10. April 2007 15:52

Rob Mensching has outlined some info on the targets for WiX v3. (http://robmensching.com/blog/archive/2007/04/10/WiX-v3-Roadmap-Draft.aspx) There's some good looking features in the roadmap. In particular I'm interested in the improved patch support and the expansion of ClickThrough support. The bootstrapper extensions sound good as well.

Congrats to the team for reaching the end of WiX v2.

Vista control with WinForms

by admin 25. March 2007 17:07

I noticed this article on The Code Project this morning. It provides wrappers to make windows forms controls appear with the new Vista styles (when used in Vista). The nice thing is that they degrade properly for use on older versions of Windows.

The other nice thing is that it provides a version of the Command Link control for use on Windows XP (see picture below). Command links are a nice addition in Vista which I've already had a few uses for. In fact I had already started my own implementation in windows forms because I hadn't found any acceptable solutions. From my quick glance at this article it looks like I might be able to throw that away and use this instead.

Command Links

Other Vista vs XP Controls

Auto-update and WiX

by admin 24. March 2007 02:42

The end of my PhD is in sight and my post-PhD plans include the possibilty of some microISV action. So I need an auto-update plan for a WinForms app. I'm using WiX to deploy the app originally since the app includes fancy things like windows services, not just xcopy.

Basically I need it to grab a WiX generated patch file. Stop the app (after prompting the user) and prompt for admin credentials (if necessary) before starting the patch.

I've been looking around and I can't find any auto-update solutions I can use out of the box. So it looks like I'm going to have to write my own :( or at least adapt one of the current solutions.

Anybody got some better ideas?

Setup Apache-Subversion on Windows XP - Part 2 - SSL

by admin 24. November 2006 07:53

Here's part two of my Apache-Subversion setup process. This time we're setting up SSL on the server.

1. Generate SSL Certificates

First up we need to generate some certificates for the system. Although OpenSSL came with the Apache install, it doesn't include all the necessary config files by default so the easiest way I found was to download a separate set of binaries which run straight out of the box. Grab OpenSSL from http://www.slproweb.com/products/Win32OpenSSL.html and install it.

The certificates are generated through the following commands:

openssl req -new -out server.csr
openssl rsa -in privkey.pem -out server.key
openssl x509 -in server.csr -out server.crt -req -signkey server.key

Fill in the appropriate info when prompted. Make sure that when you're prompted for the Common Name (eg, your websites domain name) you put in the address of the server (e.g., www.rikware.com). Note that these certificates are of course not signed by a certificate authority so you will be warned when visiting the site that they may be insecure. You can avoid this by distributing the server.crt file to your clients separately. Alternatively get the certificates signed properly, but I'm not going in to how that is done. Check out http://raibledesigns.com/wiki/Wiki.jsp?page=ApacheSSL for some more info.

You should now have a server.crt and server.key file. Copy those to the /Apache/conf/ssl.crt/ and /Apache/conf/ssl.key/ directories (you'll need to create the directories yourself.

2. Config

Open up /Apache/conf/ssl.conf and replace "c:/apache" with "C:/Program Files/Apache" (or wherever you've installed Apache. Also uncomment (remove the '#') the lines <IfDefine SSL> and </IfDefine>.

If you want to move SSL from the default port then search for "443" and replace it with the new port.

Open up /Apache/conf/httpd.conf and uncomment the following lines:

  • LoadModule ssl_module modules/mod_ssl.so
  • <IfModule mod_ssl.c> and the </IfModule> just below.

3. Test

Start up Apache and try browsing to https://localhost/. You should get the Apache default page again after an SSL.

Next time - Adding Subversion support to Apache.

Codename RikReader released

by admin 1. November 2006 12:12

My old partner in crime, Doug, has released RikReader. I'd love to say I contributed more than some beta testing and part of the name but unfortunately it's all Doug's good work. Hopefully I'll find some time to release a nice little addon utility for RikReader which we've been talking about. I won't say what because I probably won't find the time and it will never eventuate :(

Experimenting with WiX

by admin 11. October 2006 17:42

I've just tried using WiX for the first time. For those that don't know, WiX is an open source project started at Microsoft for developing installers using the Windows Installer framework. WiX is supposed to be much simpler than the standard tools, but doesn't claim to be simple.

My only previous experience has been with the installer projects built into Visual Studio, which I've found to be very confining. I'm happy to say that WiX is the exact opposite. I feel like I'll be able to do pretty much anything I want to once I fully understand WiX. The learning curve is pretty steep however.

The best site I've found for learning is the tutorial at http://www.tramontana.co.hu/wix/ Just read through it all to get a good overview then use it as a reference as you start writing your own stuff.

So far, I have just one tip to share. The tutorial shows how to create shortcuts in the start menu and desktop, but doesn't show you how to add shortcuts for all users. Luckily it's quite simple, just add this:

<Property Id="ALLUSERS">1</Property>

Of course, just connect that up to some radio buttons and allow the user to choose whether to install for themselves or everybody :)

About Us

RikWare is a software development company based in Brisbane, Australia. Led by director, Richard Mason, RikWare focusses on solving challenging business problems promptly and effectively.

RikWare is also the owner of Aussie Bushwalking, a free, wiki-like collaboration site for bushwalkers and the creator of RikMigrations, a migration framework for .NET.

Aussie Bushwalking

Want to discover some new bushwalking ideas? Share a few of your own? Aussie Bushwalking is like Wikipedia... but for bushwalkers. Head on over. Anybody can see the walks and it's free to sign up and share your own.

 

RikMigrations

RikMigrations is a database migration library for .NET based on the migrations facility provided by Ruby on Rails. It allows simple upgrading and downgrading of databases using .NET code (such as VB or C#) either from the command line or built into your application. Unlike many other migration frameworks, RikMigrations is a stand-alone library which does not rely on a large surrounding framework.