I have a class Mod, which has a member:
ICollection<Event> EventList
The class Event has a member:
public virtual EventType Type { get; set; }
The class EvenType has member:
public virtual int Id
I want to get a list of all Events in Mod which have an id of 1 or 2 using NHibernate Criteria. I did this:
var subCriteria = DetachedCriteria.For<Mod>()
.Add(Restrictions.In("Event.Type", new int[] {1, 2 })
);
criteria.Add(Subqueries.Exists(subCriteria));
but I get a runtime error saying
Cannot use subqueries on a criteria without a projection.
So, fine, but I don’t know what to put for the Projection. I can find examples of how to do Projections, but nothing that really explains the purpose. I’ve tried various things, but all result in a runtime error:
Message : Value cannot be null.
Parameter name: key
Source : mscorlib
Help link :
ParamName : key
I need to use subquery because I will be adding more when this works.
Can you suggest what to do for Projection?
As documented here: 16.4. Associations it could be done like this:
EDIT: pure Criteria API:
using detached criteria. In this case, the
EventTypemust have propertyModIdor refernece to theModinstance. In the subquery, we have to return list of valid Mod.IDs