How to query Dynamic properties in NHibernate?
I have this problem. I have a class like this:
public class WfStep
{
private readonly IDictionary properties = new Hashtable();
public virtual Guid Id { get; private set; }
public virtual string Name { get; set; }
public virtual dynamic Properties { get { return new HashtableDynamicObject(properties); } }
and its mapping file like this:
<class name="ConsoleApplication4.WfStep" table="WFSteps">
<id name="Id">
<generator class="guid"/>
</id>
<property name="Name" />
<map name="Properties" table="WFSteps_Properties" access="field.lowercase">
<key column="StepId" />
<index column="PropertyName" type="System.String"/>
<composite-element class="ConsoleApplication4.ValueItem, ConsoleApplication4">
<property name="Value" type="System.String"/>
<property name="ValueType" type="System.String"/>
</composite-element>
</map>
This example is ported from: Ayende Support dynamic fields with NHibernate and .NET 4.0
Then I save an object to the db like this: ie.
var step = new WfStep { Name = "John" };
var prop开发者_JS百科erty = new ValueItem() { Value = DateTime.Now, ValueType = "System.DateTime" };
step.Properties["DateProperty"] = property ;
Session.Save(step);
Now I want to return all the WFSteps that have DateProperty set to year 2010 ie.:
var Session.Query<WfStep>().Where(x=>x.Properties.DateProperty.Year == 2010);
It throws an error:
An expression tree may not contain a dynamic operation
How can I query this type of class that has Dynamic properties.?
You can index collections mapped as a map or as list. Try the following hql
from WfStep s
where s.Properties["DateProperty"] = "2010"
精彩评论