I am having problems handling a situation where the list of parameters sent by a named request in NHibernate is empty.
This is an example of my situation:
<sql-query name="MyClass_FilterByCategoryID"> <return alias="MyClass" class="MyProject.BusinessEntities.MyClassBE"/> <![CDATA[ SELECT DISTINCT MyClass.* FROM MyClassTable MyClass WHERE 1 = 1 AND MyClassTable.CategoryID NOT IN (:categoryIDs) ]]> </sql-query>
This is the method that is called:
public IList<MyClassBE> FilterByCategoryID(List<String> categoryIDs) { return session.GetNamedQuery("MyClass_FilterByCategoryID") .SetParameterList("categoryIDs", categoryIDs) .List<MyClassBE>(); }
However, when I pass an empty list to a method, I get this error:
System.NullReferenceException: An object reference is not set to an object instance.
Server Stack Trace:
in NHibernate.Engine.TypedValue..ctor (type IType, value Object, EntityMode entityMode) in C: \ junctions \ BS \ 3rdParty \ NHibernate.2.1.2.GA-src \ src \ NHibernate \ Engine \ TypedValue. cs: line 25
in NHibernate.Impl.AbstractQueryImpl.SetParameterList (string name, ICollection vals, type IType) in C: \ junctions \ BS \ 3rdParty \ NHibernate.2.1.2.GA-src \ src \ NHibernate \ Impl \ AbstractQueryImpl.cs: line 647
in NHibernate.Impl.AbstractQueryImpl.SetParameterList (string name, ICollection vals) in C: \ junctions \ BS \ 3rdParty \ NHibernate.2.1.2.GA-src \ src \ NHibernate \ Impl \ AbstractQueryImpl.cs: line 666
in MyProject.Dao.MyClassDao.FilterByCategoryID (List`1 categoryID) in MyClassDao.cs: line 50
What would be the best way to solve this problem? Note that a named query is, of course, much more complicated than the one presented above, so I would like to avoid copying it into a second version that does not use a parameter list.