NHibernate: Add criteria if parameter is not null

I am trying to get a list of orders based on parameters set by the user (main search function). The user enters either orderId, or a bunch of other parameters that will be wrapped in a message and, in the end, go to the method below. My question is: how can I only look at parameters that really matter? Therefore, if the user had to enter the received date range and store number, and all other fields were empty, I want to return orders for stores received in the date range and ignore all zero parameters. At first I thought I could use the conjunction, but I see no way to ignore null parameters. Then I started splitting things into if statements below the main expression, but I don't want to look at these criteria if the user provides an external interface.Is there an easy way to do this?

        public IList<Core.Order> 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<Core.Order>();
        }
        catch (NHibernate.HibernateException he)
        {
            DataAccessException dae = new DataAccessException("NHibernate Exception", he);
            throw dae;
        }
    }
+3
2

try . , db .

NHibernate.ICriteria criteria = NHibernateSession.CreateCriteria(typeof(Core.Order));

            if (!String.IsNullOrEmpty(ExternalId))
            {
                criteria.Add(Expression.Like("ExternalId", ExternalId));
            }

            if (beforeReceivedDate != null && afterReceivedDate != null)
                criteria.Add(Expression.Between("ReceivedDate", beforeReceivedDate, afterReceivedDate));

            if (beforeTransmissionDate != null && afterTransmissionDate != null)
                criteria.Add(Expression.Between("TransmissionDate", beforeTransmissionDate, afterTransmissionDate));

            if (beforeAllocationProcessDate != null && afterAllocationProcessDate != null)
                criteria.Add(Expression.Between("AllocationProcessDate", beforeAllocationProcessDate, afterAllocationProcessDate));

            if (Store.HasValue)
                criteria.CreateCriteria("Store", "Store").Add(Expression.Eq("Store.LocationNumber", Store.Value));

            return criteria.List<Core.Order>();
+2

- . , .

    private ICriteria AddSearchCriteria(ICriteria criteria, string fieldName, string value)
    {

        if (string.IsNullOrEmpty(fieldName))
            return criteria;

        if(string.IsNullOrEmpty(value))
            return criteria;

        criteria.Add(Expression.Like(fieldName, "%" + value + "%"));

        return criteria;
    }

, , :

var query = session.CreateCriteria(typeof (User));

AddSearchCriteria(query, "FirstName", form["FirstName"]);
AddSearchCriteria(query, "LastName", form["LastName"]);

var resultList = new List<User>();
query.List(resultList);
return resultList;

, , , ICriteria .

0

Source: https://habr.com/ru/post/1718824/


All Articles