2

I have a generic search method which should be able to search based on any field. I have something like this:

criteria = x => x[FieldName] == fieldValue;

This works well for strings but, how can I use it for a value of type DateTime greater than? We have tried something like this but doesn't work.

criteria = x => DateTime.Parse(x[FieldName]) >= EndDate);

Any idea?

2
  • Is there a reason why you are not strongly typing the fields to be searched against?
    – Richard Seal
    Mar 9, 2017 at 15:40
  • 1
    Simply because this is a repository that should be able to search anything. To have strong types, I need to know the type, isn't it? This is a migration from a Lucene implementation, and I can't redo the whole application. It was generic before, it is generic now. Mar 9, 2017 at 16:02

1 Answer 1

3

I think i found the answer myself. I'm not really sure why it works, but apparently it does work:

criteria = x => ((DateTime)x[(ObjectIndexerKey)FieldName]) >= StartDate;

I think the difference with the original code is: The original code is parsing the value of the field as DateTime, and then doing the comparison, while the solution, marks the field as a DataTime object, to later do the comparison. Similar, but not the same.

I'm happy to accept somebody else's answer if it includes a better explanation ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.