6
\$\begingroup\$

I have a bit of EF Lambda code which returns a list of data from the db table, then orders by a field called IsDefault

At first, the code was

var listOfData = Db
    .TableName
    .Where(u => u.UserId == userId)
    .OrderBy(u => u.IsDefault)
    .ToList();

Which when writing down, sounds correct. However is wrong as this will order by 0 > 1, and True = 1.

So I changed the statement to;

var listOfData = Db
    .TableName
    .Where(u => u.UserId == userId)
    .OrderBy(u => u.IsDefault ? 0 : 1)
    .ToList();

However, I could have also written this 2 other ways.

.OrderBy(u => !u.IsDefault)

OR

.OrderByDescending(u => u.IsDefault)

Now in my mind, u.IsDefault ? 0 : 1 reads better, and the other two could be misread as the not wanting the default value first.

What is your view on this?

\$\endgroup\$
0

3 Answers 3

9
\$\begingroup\$

I wouldn't use either ordering on the server side because it looks like the ordering only matters for displaying the data. I cannot imagine why it should matter from the programmatic point of view.

Let the client, its view, its display control, or whatever sort the items according to its needs.

\$\endgroup\$
2
  • \$\begingroup\$ Good point actually. Could just do the ordering when building up the view model. \$\endgroup\$ Commented Jan 12, 2017 at 13:46
  • \$\begingroup\$ I am going with this answer, as it is the simpler solution for my needs. Thanks for your help. \$\endgroup\$ Commented Jan 13, 2017 at 10:59
2
\$\begingroup\$

When reading the code the following is most clear and fastest to understand:

OrderBy(u => u.IsDefault ? 0 : 1)

It sounds like: "order by: if default then 0=top if not default then 1=bottom"

The following is hard to understand:

OrderByDescending(u => u.IsDefault)

It sounds like: "Oder descending by IsDefault? WTH? Which goes first default or not default? true or false? OK true=1 and false=0 so false is first so not default is first, but it is descending then non default is last so default is first. Finally I get it: Default value goes first."

\$\endgroup\$
1
\$\begingroup\$

I would go with .OrderBy(u => !u.IsDefault) the other 2 don't quite fit in my opinion.

You have 2 alternative options

  1. Declare a property that is called IsNotDefault and use it there .OrderBy(u => u.IsNotDefault) it will return !IsDefault so it flips the results.

  2. The best option would be to use enum :

    public enum MyEnum
    {
        Default,
        NonDefault,
    }
    

    Note think of a better name for that enum..

    Now you can use it just like your ternary operator but without the ternary operator :

    public MyEnum IsDefault {...}
    .OrderBy(u => u.IsDefault)
    

    You can even apply more values and it will still order them in the right way.

\$\endgroup\$
5
  • \$\begingroup\$ Yeah actually considered using Enums, will take a look. \$\endgroup\$ Commented Jan 12, 2017 at 13:46
  • \$\begingroup\$ Why the down-vote ? \$\endgroup\$
    – Denis
    Commented Jan 12, 2017 at 15:28
  • \$\begingroup\$ Not me, you still got a +1 from me. \$\endgroup\$ Commented Jan 12, 2017 at 15:31
  • \$\begingroup\$ I'm just wondering if there is anything incorrect in the answer :P \$\endgroup\$
    – Denis
    Commented Jan 12, 2017 at 15:58
  • \$\begingroup\$ As much as this is a great option. For now my needs are purely for boolean, and it would require too much of a code update for introducing the Enum. Thanks for your help. \$\endgroup\$ Commented Jan 13, 2017 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.