Working around null values in LINQ queries

I’m almost ashamed to admit I hadn’t commonly used the ‘null coalescing operator‘ ?? in C# until recently, and was commonly writing code like

var myVar = myNullableVar == null ? myNullableVar.Value : 0;

or variations on a theme using HasValue etc (still better than the long-hand if-else mind you)

Clearly this is more readable as

var myVar == myNullableVar ?? 0;

Often I find that things break down when you introduce Entity Framework, as there’s limitations on what it will understand (from the point of view of translating to the underlying data context).  Null values though are another place you can save a bit of repetitive code, as you’ll quite often have nullable dates, or other nullable types..

var output = (
    from tab in context.MyTable
    where tab.EffectiveDate == effectiveDate
    select new 
    {
        Code = tab.Code,
        /* Old
        Value = tab.Value == null ? 0 : tab.Value.Value
        */
        // New
        Value = tab ?? 0
    }).ToList();

This is a pretty simple example, but in conjunction with the SqlFunctions library, you can keep things nice and neat with type conversions in your code.

It’s only when you look a little further into the language that you see c#’s got quite a nice set of operators now 🙂

 

Book Review – Rogue Code by Mark Russinovich

Rogue Code by Mark Russinovich

Having read and reviewed Mark’s two previous ‘Jeff Aitken’ novels, Zero Day, and Trojan Horse; I was keen to read Rogue Code as soon as I heard about it.

First of all though – I want to know when the fabled movie/s are going to get made!!?

We rejoin our heroes (Jeff and Daryl – a girl btw) some time after the previous adventure, in a time where their increased reputation and success started to drive them physically apart as a couple, if not emotionally.  The two threw themselves deeper into their respective work, and we meet Frank Renkin – Jeff’s new associate, who has more than a little history at ‘the company’ himself.

Frank is probably the best new move by Russinovich, as his technical expertise, coupled with a more ‘active’ previous association with the CIA brings a new, and more physical dimension to the team, that comes in very handy indeed.

The format of the book is once again based on a diary style over the course of a hectic 10 days.

This time, the threat is on home soil, and is centred around the cut throat and bleeding edge world of high-frequency trading on the New York Stock Exchange.  Real-world examples are given around previous glitches and exploits with HFT in the ‘Flash Crash’, and IPO’s like Facebook.

The stakes are high as companies seek to gain advantage by proximity hosting of their trading code as close to the NYSE as possible.  Other, more nefarious types seek to exploit the system from the inside.

Again, the network of bad guys spreads internationally, and we learn about the complex world of modern money laundering, and how criminals have embraced uber-technical crime, due to the huge potential gains to be made.

There’s no shortage of thrills and spills, with more personal danger than ever before, while the technical detail across multiple fields again is second to none, as Russinovich’s background is bought to the fore, whilst not swamping the non-techie with unfathomable buzz words.

The book maintains a good pace throughout, and really maintains the interest as plot lines converge.  If any criticisms could be laid, they would be pretty minor, such as the occasional conspicuous re-explanation of things like Daryl’s language background, and the ‘newspaper’ articles, that felt like an interruption, just when things were ‘getting good’.  That’s all pretty nit-picky though.

All this left just one question.  Where are the movies already!?