2011-04-05 21 views
3

我有這個問題。 我有這樣一類:如何查詢NHibernate中的動態屬性?

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年即每年:

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

它拋出一個錯誤的WFSteps:

An expression tree may not contain a dynamic operation

我如何可以查詢此類型具有動態屬性的類。

回答

0

您可以索引映射爲映射或列表的集合。嘗試以下hql

from WfStep s 
where s.Properties["DateProperty"] = "2010" 
+0

問題是我不想使用hql,爲此。只有QueryOver或Linq。 – Luka 2011-05-20 07:19:46

+0

您是否嘗試過'.Where(x => x.Properties [「DateProperty」] =「2010」)? – Vadim 2011-05-20 14:58:05