[nHibernate] casting string to bool using nHibernate Criteria
- by code-zoop
I have an nHibernate query using Criteria, and I am trying to cast a string to bool in the query itself. I have done the same with casting a string to int, and that works well (the "DataField" property is "1" as a string):
var result = Session
.CreateCriteria<Car>()
.Add(Restrictions.Eq((Projections.Cast(NHibernateUtil.Int32,
Projections.Property("DataField"), 1))
.List<Car>();
tx.Commit();
But I am trying to do the same with bool, but I do not get the expected result:
var result = Session
.CreateCriteria<Car>()
.Add(Restrictions.Eq((Projections.Cast(NHibernateUtil.bool,
Projections.Property("DataField"), true))
.List<Car>();
tx.Commit();
"DataField" is the string "True", but the result in an empty list, where it should contain 100 elements with the "DataField" property string set to "True". I have tried with the string "true", and "1", but the result is still an empty List.
[EDIT]
As Commented below, I could check for the string "True" or "False", but I would say this is a more general question than just for the Boolean.
Note that the idea is to have some sort of key value representation of the data, where the value can be different data types. I need the value table to contain all data, so storing the data as string seems like the cleanest solution!
I have been able to use the method above to store both int and double as string, and to the cast in the query, but I have not succeeded using the same method for DateDime and Boolean.
And for DateTime it is crucial to have the actual DateTime object.
How can I make the cast from string to bool, and string to DateTime work in the queries?
Thanks