Open kahanu opened 6 years ago
Ok, I got it to work with a bit of a code smell in my opinion, but it does work. If anyone has any suggestions on how to make this work more efficiently, it would be greatly appreciated.
private void FilterStatementBuilder<T>(IFilter filter, List<SearchCriteria> list)
{
foreach (var criteria in list)
{
if (criteria.Type == "Decimal")
{
Decimal fieldValue = Convert.ToDecimal(criteria.FieldValue);
filter.By(item, Operation.ByName(criteria.OperationName), fieldValue, default(Decimal), criteria.Connector);
} else if (criteria.Type == "DateTimeOffset")
{
DateTimeOffset fieldValue = new DateTimeOffset(DateTime.Parse(criteria.FieldValue.ToString()));
filter.By(item, Operation.ByName(criteria.OperationName), fieldValue, default(DateTimeOffset?), criteria.Connector);
} else if (criteria.Type == "Int16")
{
Int16 fieldValue = Convert.ToInt16(criteria.FieldValue);
filter.By(item, Operation.ByName(criteria.OperationName), fieldValue, default(Int16), criteria.Connector);
}
else
{
// String
filter.By(item, Operation.ByName(criteria.OperationName), criteria.FieldValue, null, criteria.Connector);
}
}
}
This would be the new JSON request:
{
"SearchCriteria": [
{ "FieldName": "Phone", "OperationName": "EqualTo", "FieldValue": "8183332222", "Type": "String" },
{ "FieldName": "FirstName", "OperationName": "EqualTo", "FieldValue": "King", "Type": "String" },
{ "FieldName": "To", "OperationName": "GreaterThan", "FieldValue": "2018-02-09T14:34:24", "Type": "DateTimeOffset" },
{ "FieldName": "ContributionAmount", "OperationName": "GreaterThan", "FieldValue": 100.00, "Type": "Decimal" }
]
}
Hi @kahanu,
First of all, thanks for using the library, and I'm sorry for the really late, late, late reply. π’
From what you described, it seems to me that you're getting this error because you were always using string
values while creating the FilterStatement
. Which would explain why your workaround, well... works. π
If that's really the case, I've got two suggestions for you:
else if
:
var type = (TypeCode)Enum.Parse(typeof(TypeCode), criteria.Type);
var value = criteria.FieldValue;
var result = Convert.ChangeType(value, type); filter.By(item, Operation.ByName(criteria.OperationName), result, connector: criteria.Connector);
2. Or, you could simply cast the value to `object` (we have tests showing how this works π)
Anyway, please let me know if those suggestions were of any use.
Thanks.
Hi David,
Thanks for the response. I actually fixed it a while ago, but Iβll see if your solution works better. Thanks again for making this, it has helped me build a flexible search solution for my app.
Thanks,
King Wilder king@gizmobeach.commailto:king@gizmobeach.com 818 384-4438 Skype: king.wilder
From: David Belmont notifications@github.com Sent: Friday, January 4, 2019 5:58 AM To: dbelmont/ExpressionBuilder ExpressionBuilder@noreply.github.com Cc: King Wilder info@kingwilder.com; Mention mention@noreply.github.com Subject: Re: [dbelmont/ExpressionBuilder] The binary operator GreaterThan is not defined for the types 'System.Decimal' and 'System.Double'. (#31)
Hi @kahanuhttps://github.com/kahanu,
First of all, thanks for using the library, and I'm sorry for the really late, late, late reply. π’
From what you described, it seems to me that you're getting this error because you were always using string values while creating the FilterStatement. Which would explain why your workaround, well... works. π
If that's really the case, I've got two suggestions for you:
var type = (TypeCode)Enum.Parse(typeof(TypeCode), criteria.Type);
var value = criteria.FieldValue;
var result = Convert.ChangeType(value, type);
filter.By(item, Operation.ByName(criteria.OperationName), result, connector: criteria.Connector);
Anyway, please let me know if those suggestions were of any use.
Thanks.
β You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/dbelmont/ExpressionBuilder/issues/31#issuecomment-451450116, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAwehvkPMtmfPLA8szCdaevAnXcJLdhVks5u_13OgaJpZM4VKVji.
I'm getting this message when passing in a number, and I also get a similar error message for DateTimeOffset. Are there any work arounds, or fixes for this? Otherwise, your assembly is fantastic. Saved me hours of work.
BTW, I'm using this in a WebApi project. It consumes a JSON object with an array of objects similar to the FilterStatement class, and then I loop through the list and create the filter.By() collection. It all works perfectly except for the DateTimeOffset types and the above mentioned types.
The SQL Server database table has columns using the smallint, that also seems throw the same exception. Anyway, any help is appreciated.
Here's some code - my Angular client will send a JSON object to the WebApi service and the object will be used as the request object in the C# method:
Then in my WebApi method, I pass the request object to a helper method that will loop through all the objects in the SearchCriteria collection, and create the filter.By() collection:
This is the simple helper function to create the filter.By() collection:
In the JSON objects, I've included the Type property hoping I could use that to coerce the data type for the property, but I don't know if I can do this here. Any thoughts?
Also, if I could be doing this is a better way, I'm open to suggestions. Thanks.