Querying Entity Framework. Part 24 – Aggregating Nullable Fields

It’s a very simple case for Entity Framework model. I have a nullable column in Clients table.

I want to count values and to sum up them. The LINQ query to EF model can be very simple.

But these queries reveals some magic. Count() returns int type, but Sum() return int?. So you may fall into one of the following cases.

Method Return type Result on all rows Result on not-nullable rows Result on empty dataset
Count() int Count all rows regardless null or not null Count only not-nullable rows Zero
Sum() int? Sum only not-nullable values Sum only not-nullable values Null

The most dangerous is the last case when Sum() returns null, so  you should add a check for null value. If you want to get zero, you need to change a request and add DefaultIfEmpty() method.

Unit Testing C# Synchronous Methods

When you need to unit test a method, you should check happy path (for example, the method returns a resulting value) and sad path (the method throws exception). Here I describe a basic usage of unit testing with Visual Studio 2017 and xUnit version 2.2.

The code being tested:

1. Check successful result (happy path) – it’s very straightforward.

2. Check fail when the method throws an exception (sad path)

If you have faced with MSTest, you might remember [ExpectedException] attribute. In this case MSTest waits for a particular exception would be thrown in a whole unit test method, but not in a specific line of code. Modern unit test frameworks have more graceful capabilities to catch the exception.

2.1. Using Throws<>

But this approach combines Act and Assert phases of unit test in one line of code. Richard Banks suggested a better way in his article Stop Using Assert.Throws in Your BDD Unit Tests.

2.2. Using Record.Exception

At first, I check that the exception was really caught, then check the type of that exception.

Next time I’ll tell about unit testing the asynchronous methods.

Code Highlighting In WordPress.org Blog

I’ve found a very pretty WordPress.org plugin Crayon Syntax Highlighter written by Aram Kocharyran (http://aramk.com). The first impression is very good, it makes the appearance of C# or T-SQL code in blog posts really better. Thanks, Aram!

If you have a WordPress.org blog and install this plugin, you need to change your posts, so the code would be shown correctly (for example, angle brackets, i.e. “greater than” or “less than” mathematical signs). The most important is to change <pre> tags, so they should include class and decode attributes that reflect programming languages. So the tag in default configuration should look like <pre class=”lang:c# decode:true ” > for C# or <pre class=”lang:tsql decode:true ” > for Transact-SQL code.

Windows 8.1 Touchpad Driver for ASUS Notebooks

I’m working on Asus K53sd notebook. After upgrade to Windows 8.1 (mainly to use Hyper-V) I’ve faced some problems with touchpad – I can’t disable it in gracious manner except disabling it totally at driver settings level. I’ve tested all drivers that are listed on product support page. Beside that I could not disable touchpad when I plugg in a mouse.

It took me some time to find the right link. It’s a page devoted to touchpad. And it really works!

I don’t know why they did not include it in product page. Maybe it’s a question for some scientists 😉

Link to Asus touchpad driver http://support.asus.com/Download.aspx?SLanguage=en&m=Touchpad&os=30

Hope the driver for Windows 10 will be there…

[UPDATE 2016-05-31]
That’s incredible, but Asus had changed the search engine, so the link above does not find anything. But Elantech driver for Windows 8.1 was found on another page http://www.asus.com/support/Download/3/589/0/21/41/

The direct link for version is http://dlcdnet.asus.com/pub/ASUS/nb/DriversForWin8.1/Touchpad/Touchpad_Elantech_Win81_64_VER115162.zip

I’ve installed it on Windows 10, and yes, Fn + F9 disables notebook touchpad.