NHibernate - 将where子句添加到映射到列表的列

时间:2013-11-12 20:17:51

标签: nhibernate nhibernate-criteria

通过实施ICompositeUserType,大约有100列已映射到IList<IdentifiedValue>。这是一个包含列名称及其值的对象。

public class IdentifiedValue {
    public string Name;
    public object Value;
}

public class CompanyContainer {
    public virtual IList<IdentifiedValue> Values;
}

我现在如何过滤其中一列?

string columnToFilter = "Col1";
object needle = "12345A";
IQueryOver<CompanyContainer, CompanyContainer> query = ...

// This crashes (as somewhat expected)
query.Where(cc => cc.Values.Single(attr => attr.Name == columnToFilter).Value == needle);

// I sort of expected this to work:
query.Where(Restrictions.Eq(columnToFilter, needle));

不幸的是,这些方法不起作用。

流向IList的映射如下:

var columnDefinitions = new string[] {"Col1", "Col2"};

var cols = Map(x => x.Values)
      .CustomType<IdentifiedValueMapper>()
      .Columns.Clear();

foreach (var column in columnDefinitions) {
    cols.Columns.Add(column.Name)
}


public class IdentifiedValueMapper : ICompositeUserType {
    public string[] PropertyNames
    {
        get
        {
            var result = new List<string>();
            foreach (var column in columnDefinitions) {
                result.Add(column.Name);
            }
            return result.ToArray();
        }
    }

    public IType[] PropertyTypes
    {
        get
        {
            var result = new List<IType>();
            foreach (string columnName in columnDefinitions) {
                 result.Add(NHibernateUtil.Int32);
            }
        }
    }

    public Type ReturnedClass
    {
        get { return typeof (List<IdentifiedValue>); }
    }
}

0 个答案:

没有答案