MightyOrm / Mighty

A new, small, dynamic micro-ORM. Highly compatible with Massive, but with many essential new features.
BSD 3-Clause "New" or "Revised" License
101 stars 20 forks source link

Select All with AND , Or, Like #35

Closed oghenez closed 3 years ago

oghenez commented 3 years ago

Hello,

I started trying out Mighty, with AND filtering

var personList = db.All("Created >=@0 AND Created <=@0", new DateTime(2020, 1, 1), new DateTime(2020, 12, 31));

but seems to be getting this error

Argument 2: cannot convert from 'System.DateTime' to 'string' Argument 3: cannot convert from 'System.DateTime' to 'string'

I am using version 3.1.3.0

Any ideas on how to perform these conditional filtering

mikebeaton commented 3 years ago

Apologies for the delayed reply, the syntax for Query (i.e. arbitrary query from arbitrary table, with optional arguments) is indeed just what you've used. The syntax for All (all items from 'current' table, as defined in the MightyOrm object constructor; with optional where, orderBy, columns and limit) requires you to either put in explicit values for the optional arguments, so that you get to where the params args start, or to initialise an array inline for the named args argument, i.e. one or other of:

var personList = db.All("Created >= @0 AND Created <= @1", null, null, 0, null, new DateTime(2020, 1, 1), new DateTime(2020, 12, 31));

or:

var personList = db.All("Created >= @0 AND Created <= @1", args: new object[] { new DateTime(2020, 1, 1), new DateTime(2020, 12, 31) });

The second I would say is preferable, once you understand that that's the right syntax.

However, the reason that this is the right syntax is a C# issue at least as much as it is a Mighty issue!

This is because Mighty, just like Massive, relies heavily on optional arguments (the ones with default values) and params arguments (the one where an arbitrary number of extra values can be provided at the end of an argument list). Unfortunately, in methods which need to use both (in order to provide you with everything you might need in that case, including All) then you cannot have all the advantages of both - and to pass the params args one or other of the above two syntaxes have to be used, neither of which is ideal compared to the very simple syntax e.g. of Query.