I recently was working with some dynamic Search Expressions and ran into a bit of an issue when I was attempting to search for multiple Date/Times.
My SearchExpression constructors resemble the following (some omitted for brevity):
public SearchExpression(string propertyName,
ComparisonOperator comparisonOperator,
object value) {...}
and
public SearchExpression(string propertyName,
ComparisonOperator comparisonOperator,
object[] values,
BooleanOperator innerBooleanOperator) {...}
Both of these work as they should when passed the following :
However, when it comes to passing DateTime information, I began to encounter some problems.
I have all of my data seperated when it comes in based on the type (DateTime, Numerical or String) and check for validity, they are then stored into array to pass to the proper expressions, like so:
object[] stringParameters;
object[] numericalParameters;
DateTime[] dateParameters;
All of these work when I pass in any of the above, with the exception of an array of DateTime parameters.
So my question, which leaves me feeling a bit foolish, is why can I pass a single DateTime value as an object, but am unable to pass an DateTime[] as an object[] .
Example:
DateTime[] dateParameters;
//This works fine
new SearchExpression("DateTime", ComparisonOperator.Equals, dateParameters[0]);
//This fails to work
new SearchExpression("DateTime", ComparisonOperator.Equals, dateParameters,
BooleanOperator.Or);
Why can I pass a single DateTime value as an object?
There is an implicit boxing conversion available from DateTime (a value-type) to object.
But am unable to pass an DateTime[] as an object[]?
There is no implicit conversion available from a DateTime[] to object[]. Although arrays in C# exhibit covariance in general (for example, there is an implicit conversion from string[] to object[]), this does not extend to arrays of value-types - covariant conversions are currently required to be identity-preserving conversions, with good reason.
This is inherently a problem of Covariance which arrays do not support arrays do not support for value types.
Eric Lippert has a very good post on that here.
Change method signature to IEnumerable<object>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With