3

この問題があります。私はこのようなクラスを持っています:

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); } }

そしてそのマッピングファイルは次のようになります:

<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>   

この例は次のものから移植されています: Ayende Support dynamic fields with NHibernate and .NET 4.0

次に、次のようにオブジェクトをデータベースに保存します。

var step = new WfStep { Name = "John" };
var property = new ValueItem() { Value = DateTime.Now, ValueType = "System.DateTime" };
step.Properties["DateProperty"] = property ;

Session.Save(step);

ここで、DateProperty が 2010 年に設定されているすべての WFStep を返したいと考えています。

var Session.Query<WfStep>().Where(x=>x.Properties.DateProperty.Year == 2010);

エラーがスローされます:

式ツリーに動的操作が含まれていない可能性があります

動的プロパティを持つこのタイプのクラスを照会するにはどうすればよいですか?

4

1 に答える 1

0

マップまたはリストとしてマップされたコレクションにインデックスを付けることができます。次のhqlを試してください

from WfStep s
where s.Properties["DateProperty"] = "2010"
于 2011-05-19T20:36:56.160 に答える