How can you concatenate datetimeoffset in Linq to SQL?

I have a Linq request that is ordered using datetimeoffest. The goal is to have the one that has NULL at the top, followed by the latter, the 2nd last, and so on. I started with this.

orderby item.Date descending

      

Doing it like this, NULLs go to the bottom. So I changed it to this.

orderby (item.Date.HasValue ? item.Date.Value.Ticks : long.MaxValue) descending

      

This works in memory queries, but does not translate to SQL. My last try is this.

orderby (item.Date.HasValue ? item.Date : new DateTimeOffset(new DateTime(9999, 09, 31))) descending

      

The problem is that max datetimeoffset is not the same as SQL and C #. I feel like I am missing an obvious simple solution.

Any input?

+2


source to share


2 answers


When using datetime, I noticed that you cannot use DateTime.Max and pass this to SQL. It seems the max time in SQL is less than C #. I assumed that applies in DateTimeOffset. This does not seem to be the case. It would seem that this code works.



orderby (item.Date.HasValue ? item.Date : DateTimeOffset.MaxValue) descending

      

0


source


Have you tried the C # coalesce operator?



orderby (item.Date ?? DateTimeOffset.MaxValue) descending

      

+2


source







All Articles