Linq

I’ve just found out how to combine .Sum() with a where clause, and all it took was a further step into the murky world of Linq.

I got my tip-off from a fellow BlogEngine.NET blog user Gary Kilminster – http://cunningplan.co.uk/post/Extension-Methods.aspx – although it doesn’t look like his blog has been updated for a while.

I had a list of custom objects, and wanted to get the sum of all their ‘Cost’ values as long as they had a specific property in common. The bit of Gary’s code that really made it click for me was:

        string londonNames = (from c in db.Customers
where c.City == "London"
select c.CompanyName).Sum();

In my case, it became:

        int result = (from o in objectList
where o.OptionGroupId == specificId
select o.Cost).Sum();

It’s just so simple and easy. Hooray!

.Net Nooks and Crannies

I really love Stack Overflow. It’s so cool to have a link to many, many great coding minds to help with the minutiae of my learning process, whether through asking a question myself or finding that someone else asked something similar months ago.

The last question I asked was about public properties that should be read-only (usually List<T> or somesuch thing), but allow the user to add an item still. The answer was to use a class called ‘ReadOnlyCollection<T>’. This is one of many, many classes that I’ve never heard of but would be incredibly useful if I had!

When I get to the end of the first run of Wargame Tools, I’ll definitely be refactoring it (especially since I plan to port the logic to an ASP.NET MVC 2.0 application and see how those work) and will be sure to blog about this class and how I find using it.

Wargame Tools – a learning program

My name is Pete, and I am a wargamer. I am also a programmer. As a programmer and huge geek, I look for ways to apply programming knowledge to my other hobbies. So it only makes sense that I would try to apply it to wargaming.

Over the years, I’ve seen, used and abandoned various ‘army list’ programs. My favourite for the longest time was Army Builder but their licence restrictions are just a little bit harsh for me – apparently, a recent ‘minor’ upgrade means that anyone without an active licence (mine ran out years ago) must buy a new one, and the datafiles are not compatible with previous versions. It sounds more like a major version change than a minor one to me, but that’s their call at the end of the day.

So I’ve decided to build my own, as daunting a task as it may be, and see if I can bring something I work on in my spare time to the web as a useful addition to the choices around in the world today. At the same time (and extending the possible release by an indeterminate amount) I decided to use this as an excuse to learn WPF and apply some more object-oriented concepts to my code.

One of the biggest stumbling blocks, as Army Builder has always worked with and Army Roster discovered this year, is that it’s not nice to just give away the intellectual property of games manufacturers. So there is a need to give people the tools they need to make the data files, with which to build the actual army lists. It’s also pretty difficult to take what amounts to a domain-specific language that your datafile program writes and your army list program understands, and present an easy, graphical way for your user to interact with it without getting them too deep into the technical sides of it. It needs to be accessible for non-programmers.

So far, I’ve been working away on it slowly, and through evenings and weekends when I can snatch a few minutes. I’ve found WPF and XAML to be very, very interesting to work with but there’s a bit of a learning curve to get over if (like me) you’re coming from a Windows Forms background. I will be putting up anything interesting that I’ve learned, or link to samples that have helped me, whenever I find something useful.