I just came across a small problem where i had a table with 2 priority columns.
One is generated by the system and the other enabled users to manually override the generated priority.

I just came across a small problem where i had a table with 2 priority columns.

One is generated by the system and the other enabled users to manually override the generated priority.

The requirement was to sort the data by the highest of the 2 columns. The problem is the manual priority might be null.

To achieve this i found out about the very useful statement in LINQ called let.

The let statement allows you to define variables within your LINQ statement.

In the example below the first let statement handles the possible null column, the second finds the highest of the two allowing us to use it for sorting.

Check it out here:


var result = from c in dal.cases
        let sortOrderManual = c.ManualPriority == null ? 0 : c.ManualPriority
        let sortOrderHighest = c.Priority > sortOrderManual ? c.Priority : sortOrderManual
        orderby sortOrderHighest descending
        select c;

You may also like

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.