bchavez / RethinkDb.Driver

:headphones: A NoSQL C#/.NET RethinkDB database driver with 100% ReQL API coverage.
http://rethinkdb.com/api/java
Other
383 stars 134 forks source link

OrderBy - multiple fields #122

Closed mattgi closed 6 years ago

mattgi commented 6 years ago

Given a dynamic array of sort parameters, how does one construct what is to be passed into OrderBy()?

e.g. .OrderBy(doc => BuildOrder(doc, sortParameters)) where sortParameters would take the format:

[
  { Direction : "Descending", Key: "lastName" },
  { Direction : "Ascending", Key: "firstName" },
]

Attempts thus far have focused on trying to chain R.Desc() to ReqlExpr without success.

Can't really find any information on how to do this in Java or C# doco or source code so any ideas would be awesome.

bchavez commented 6 years ago

Hi @mattgi,

Thank you for your question. I hope I don't come off discouraging but the best place to get help for free is to ask questions on the RethinkDB Slack channel here: https://github.com/bchavez/RethinkDb.Driver#getting-help

Please feel free to tag me in your question in Slack and I'll try to answer it as best I can when I have some free time.

The GitHub issue tracker here is primarily reserved for source-code related issues.

Thanks, Brian

:walking_man: :walking_woman: Missing Persons - Walking in L.A.

mattgi commented 6 years ago

Not a problem. I saw the getting help, signed up to slack last night, and then saw that there wasnt a specific group for C# discussion so came back here. Will just post in help I guess. Thanks @bchavez

MetaFight commented 5 years ago

@mattgi I'm facing the same issue. Did you find a solution?

bchavez commented 5 years ago

Just for completeness, here is the solution we worked out in Discord. For those edge cases where you need more than four OrderBy( arg1, arg2, arg3, arg4, ...) params:

void Main()
{
   var r = RethinkDb.Driver.RethinkDB.R;
   var conn = r.Connection()
               .Hostname("localhost")
               .Connect();

   var query = r.Db("test").Table("data").OrderBy(r.Desc("Name"), r.Asc("Age"));

   var result = query.RunResult<JArray>(conn);

   result.Dump();
}
//OrderBy params
public static class QueryHelpers
{
   public static OrderBy OrderBy(this ReqlExpr t, params ReqlExpr[] args)
   {
      var a = new Arguments(t);

      foreach (var exp in args)
      {
         a.CoerceAndAdd(exp);
      }
      return new OrderBy(a);
   }
}

Hope that helps those in the future who stumble across the issue.

:car: :blue_car: "Let the good times roll..."