Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate How do I query against an IList<string> property?
    primarykey
    data
    text
    <p>I am trying to query against an IList&lt;string&gt; property on one of my domain classes using NHibernate. Here is a simple example to demonstrate:</p> <pre><code>public class Demo { public Demo() { this.Tags = new List&lt;string&gt;(); } public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual IList&lt;string&gt; Tags { get; set; } } </code></pre> <p>Mapped like this:</p> <pre><code>&lt;class name="Demo"&gt; &lt;id name="Id" /&gt; &lt;property name="Name" /&gt; &lt;bag name="Tags"&gt; &lt;key column="DemoId"/&gt; &lt;element column="Tag" type="String" /&gt; &lt;/bag&gt; </code></pre> <p></p> <p>And I am able to save and retrieve just fine. Now to query for instances of my domain class where the Tags property contains a specified value:</p> <pre><code>var demos = this.session.CreateCriteria&lt;Demo&gt;() .CreateAlias("Tags", "t") .Add(Restrictions.Eq("t", "a")) .List&lt;Demo&gt;(); </code></pre> <p>Results in the error: collection was not an association: Demo.Tags</p> <pre><code>var demos = (from d in this.session.Linq&lt;Demo&gt;() where d.Tags.Contains("a") select d).ToList(); </code></pre> <p>Results in the error: Objct reference not set to an instance of an object.</p> <pre><code>var demos = this.session.CreateQuery("from Demo d where :t in elements(d.Tags)") .SetParameter("t", "a") .List&lt;Demo&gt;(); </code></pre> <p>Works fine, but as my real domain class has many many properties, and I am building a complicated dynamic query, doing ugly string manipulation is not my first choice. I'd much rather use ICriteria or Linq. I have a user interface where many different possible search criteria can be entered. The code that builds up the ICriteria right now is dozens of lines long. I'd really hate to turn that into HQL string manipulation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload