alirezanet/Gridify

Ordering with nullable types

reisi-h opened this issue · 4 comments

Details

i can't find any feature to support order fields with send null values to end of list.
in ef for example we can simple write query.OrderByDescending(p => p.OrderDate.HasValue).ThenBy(p => p.OrderDate)
in this case OrderDate is nullable type so null values go to end of list

please add this feature to Gridify

Hi,
So If I understand correctly you want something like the bellow output?

SELECT *
FROM [Users] AS [u]
ORDER BY CASE
    WHEN [u].[CreateDate] IS NOT NULL THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END DESC, [u].[CreateDate]

that comes from

var expected = _dbContext.Users
        .OrderByDescending(q => q.CreateDate.HasValue)
        .ThenBy(w => w.CreateDate).ToQueryString();

I'll do a little investigation about this soon.

A temporary workaround could be combining the gridify query with a static LINQ :

e.g

   [Fact]
   public void OrderBy_NullableValues()
   {
      // Arrange
      var expected = _dbContext.Users.OrderByDescending(q => q.CreateDate.HasValue).ThenBy(w => w.CreateDate).ToQueryString();
      var gq = new GridifyQuery() {OrderBy = "CreateDate"};

      // Act
      var actual = _dbContext.Users
                     .OrderByDescending(q => q.CreateDate.HasValue)
                     .ApplyOrdering(gq, startWithThenBy: true).ToQueryString();

      // Assert
      Assert.Equal(expected,actual);
   }

you can hard code this part .OrderByDescending(q => q.CreateDate.HasValue) using LINQ and use startWithThenBy parameter to add your dynamic ordering.

This feature added in v2.7.0 (Docs):
for achive the bellow query :

query.OrderByDescending(p => p.OrderDate.HasValue).ThenBy(p => p.OrderDate)

you can use the ? character after OrderDate:

query.ApplyOrdering("OrderDate? desc,  OrderDate")

Dear Mr Sabouri
I just say Thanks you very very much for your rapid respond and add this feature to this package.
آقا دم شوما گرم