title: NHibernate: Add criteria if param not null
body: <p>I'm trying to retrieve a list of orders based on parameters specified by a user (basic search functionality). The user will enter either an orderId or a bunch of other params, those will get wrapped up into a message, and eventually make their way to the method below. My question is, how do I only look at the parameters that actually have values? So if a user were to enter a received date range and a store number and all other fields were null, I want to return orders for stores received in the date range and ignore all the null parameters. At first I was thinking I could use a conjunction, but I can't see a way to ignore the null parameters. Then I started splitting things out into the if statements below the main expression, but I don't want to look at those criteria if the user provides an externalId. Is there a simple way to do this?</p>
  
  <pre><code>        public IList&lt;Core.Order&gt; GetOrderByCriteria
          (
          string ExternalId,
          int? Store,
          int? Status,
          DateTime? beforeTransmissionDate, DateTime? afterTransmissionDate,
          DateTime? beforeAllocationProcessDate, DateTime? afterAllocationProcessDate,
          DateTime? beforeReceivedDate, DateTime? afterReceivedDate
          )
      {
          try
          {
              NHibernate.ICriteria criteria = NHibernateSession.CreateCriteria(typeof(Core.Order))
                  .Add(Expression.Or
                          (
                          Expression.Like("ExternalId", ExternalId),
                          Expression.Conjunction()
                              .Add(Expression.Between("ReceivedDate", beforeReceivedDate, afterReceivedDate))
                              .Add(Expression.Between("TransmissionDate", beforeTransmissionDate, afterTransmissionDate))
                              .Add(Expression.Between("AllocationProcessDate", beforeAllocationProcessDate, afterAllocationProcessDate))
                          )
                       );
  
              if(Store.HasValue)
                  criteria.Add(Expression.Eq("Status", Status));
  
              if(Status.HasValue)
                  criteria.Add(Expression.Eq("Store", Store));
  
  
              return criteria.List&lt;Core.Order&gt;();
          }
          catch (NHibernate.HibernateException he)
          {
              DataAccessException dae = new DataAccessException("NHibernate Exception", he);
              throw dae;
          }
      }
  </code></pre>