Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Nullable not supported in sortBy in query/ linq computation expressions #11446

Closed
smoothdeveloper opened this issue Apr 18, 2021 · 1 comment

Comments

@smoothdeveloper
Copy link
Contributor

The following C# works out of the box

using System;
using System.Collections.Generic;
using System.Linq;
public record Data(DateTime? date);
public class C {
    public IEnumerable<Data> M() {
        var datum = new []{new Data(DateTime.Now),new Data(null)};
        var result = from d in datum orderby d.date select d;
        return result;
    }
}

I'd like the same to work in F#

open System
type Data = {date: DateTime Nullable }
let datum = [|{date = Nullable DateTime.Now}; {date = Nullable()}|]
let q = query {
    for d in datum do
      sortBy d.date
      select d
  }

but it gives:

error FS0001: The type 'Nullable' does not support the 'comparison' constraint. For example, it does not support the 'System.IComparable' interface

If it doesn't come as default, is it something that can be extended by overloading SortBy implementation or should be added to FSharp.Core?

member _.SortBy (source: QuerySource<'T, 'Q>, keySelector: 'T -> 'Key) : QuerySource<'T, 'Q> when 'Key : equality and 'Key : comparison =
QuerySource (Enumerable.OrderBy(source.Source, Func<_, _>(keySelector)))

@smoothdeveloper
Copy link
Contributor Author

I now see sortByNullable below in the code is a separate function and also in the query expr doc, I guess that's the way to do it if it can't be an overload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant