Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter on joins in nHibernate
    primarykey
    data
    text
    <p>I’m trying a build an nHibernate criteria expression to perform a search. </p> <p>Given the following data model:</p> <ul> <li>An operation may have zero or more sessions. </li> <li>An operation may be of zero of more operation types.</li> </ul> <p>I want to search for all sessions based on the following criteria:</p> <ul> <li>(Mandatory) Where the operation IsActive flag is true, IsPublished flag is true</li> </ul> <p>o (Optionally) and where the operation state/end dates are within the user specified date range</p> <p>o (Optionally) and where the session regionid matches a user specified id</p> <p>o (Optionally) and where the session division matches a user specified id</p> <p>o (Optionally) and where the Operation.OperationTypes are in a user specified list of type ids</p> <p>I would express this in sql as (given all optional parameters have been supplied):</p> <pre><code>SELECT [Session].* FROM [OperationTypeOperation] LEFT OUTER JOIN [Operation] ON [OperationTypeOperation].[OperationId] = [Operation].[OperationId] RIGHT OUTER JOIN [Session] ON [Operation].[OperationId] = [Session].[OperationId] WHERE ([Operation].[IsPublished] = 1) AND ([Operation].[IsActive] = 1) AND ([Session].[RegionId] = 66) AND ([Session].[DivisionId] = 99) AND ([Operation].[AdvertisingStartDate] &lt; GETDATE()) AND ([Operation].[AdvertisingEndDate] &gt; GETDATE()) AND ([OperationTypeOperation].[OperationTypeId] IN (1, 2, 3)) </code></pre> <p>And in my nHibernate query as:</p> <pre><code>public PagedResult&lt;Session&gt; Search(int? regionId, int? divisionId, DateTime? startDate, DateTime? endDate, IList&lt;int&gt; operationTypeId, int itemsPerPage, int page) { var criteria = _session.CreateCriteria(typeof(Session)) .Add(Expression.Eq("IsActive", true)) .Add(Expression.Eq("AcceptingApplications", true)) .AddOrder(new Order("StartDate", false)) ; if (regionId.HasValue) criteria.Add(Expression.Eq("Region.Id", regionId.Value)); if (divisionId.HasValue) criteria.Add(Expression.Eq("Division.Id", divisionId.Value)); if (startDate.HasValue) criteria.Add(Expression.Ge("StartDate", startDate.Value)); if (endDate.HasValue) criteria.Add(Expression.Le("EndDate", endDate.Value)); //Add the operation types if (operationTypeId.Count &gt; 0) { var operationTypes = new Collection&lt;OperationType&gt;(); foreach (int id in operationTypeId) { operationTypes.Add(_session.Get&lt;OperationType&gt;(id)); } //Join on the operations criteria.CreateCriteria("Operation") .Add(Expression.Eq("IsPublished", true)) .Add(Expression.Eq("IsActive", true)) .Add(Expression.Le("AdvertisingStartDate", DateTime.Now)) .Add(Expression.Ge("AdvertisingEndDate", DateTime.Now)) .CreateAlias("OperationTypes", "operationTypes", JoinType.InnerJoin) .Add(Expression.In("OperationTypes", operationTypes)) .SetResultTransformer(new DistinctRootEntityResultTransformer()) ; } else { //Join on the operations criteria.CreateCriteria("Operation") .Add(Expression.Eq("IsPublished", true)) .Add(Expression.Eq("IsActive", true)) .Add(Expression.Le("AdvertisingStartDate", DateTime.Now)) .Add(Expression.Ge("AdvertisingEndDate", DateTime.Now)) ; } return criteria.ToPagedResult&lt;Session&gt;(itemsPerPage, page); } </code></pre> <p>My nHibernate function is complaining about the operation types and throwing an exception “Cannot use collections with InExpression”. Plus I’m not sure if I’m filtering on the joined tables correctly. Can anyone weigh in the correct way to write the above sql as an nHibernate expression?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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