【问题标题】:How can I query a list of key value pairs using NHibernate ICriteria?如何使用 NHibernate ICriteria 查询键值对列表?
【发布时间】:2015-10-24 16:04:26
【问题描述】:

我有一个包含多个字段的类,其中一个是 IList<KeyValuePair<string, string>>

public class Foo
{
  public IList<KeyValuePair<string, string>> Bars { get; set; }
}

我正在使用 Fluent NHibernate,该特定字段映射如下:

HasMany(x => x.Bars).Component(Bar.Map);

public class BarMap : ComponentMap<KeyValuePair<string, string>>
    {
        public BarMap()
        {
            Map(x => x.Key);
            Map(x => x.Value);
        }

        public static void Map(CompositeElementPart<KeyValuePair<string, string>> part)
        {
            part.Map(x => x.Key);
            part.Map(x => x.Value);
        }
    }

使用 ICriteria API,我希望能够选择 Bars 包含键值对 { X, Y } 的所有 Foo,并且 X 和 Y 值的匹配不区分大小写。我该怎么做?

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate nhibernate-criteria icriteria


    【解决方案1】:

    IDictionary的查询方式在这里详细介绍

    并在此处记录

    17.1.4.1. Alias and property references

    Description                     Syntax                Example
    A collection key                {[aliasname].key}     ORGID as {coll.key}
    The id of an collection         {[aliasname].id}      EMPID as {coll.id}
    The element of an collection    {[aliasname].element} XID as {coll.element}
    

    所以,我们可以这样做

    .Add(Restrictions.Eq("Bars.elements", searchedValue));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多