Search code examples
c#nhibernatefluent-nhibernatenhibernate-criteriaicriteria

How can I query a list of key value pairs using NHibernate ICriteria?


I have a class with a number of fields, one being an IList<KeyValuePair<string, string>>.

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

I'm using Fluent NHibernate and that particular field is mapped as follows:

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

and

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

Using the ICriteria API, I would like to be able to select all Foo where Bars contains a key value pair { X, Y }, and for the matching of X and Y values to be case insensitive. How can I do this?


Solution

  • The way how to query IDictionary is in detail described here

    and documented here

    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}
    

    So, we can do something like this

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