3

ユーザーが指定したパラメーターに基づいて注文のリストを取得しようとしています (基本的な検索機能)。ユーザーは orderId またはその他のパラメーターのいずれかを入力します。これらはメッセージにまとめられ、最終的に以下のメソッドに進みます。私の質問は、実際に値を持つパラメーターのみをどのように見るのですか? したがって、ユーザーが受け取った日付範囲と店舗番号を入力し、他のすべてのフィールドが null の場合、日付範囲で受け取った店舗の注文を返し、すべての null パラメーターを無視したいと考えています。最初は接続詞を使用できると思っていましたが、null パラメーターを無視する方法がわかりません。次に、メインの式の下にある if ステートメントに分割し始めましたが、ユーザーが externalId を提供する場合は、これらの条件を確認したくありません。

        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;
        }
    }
4

2 に答える 2

2

最終的には、接続全体を削除し、try ブロック内のコードを以下のコードに置き換えました。また、データベースへのアクセス数を減らし、必要なコードの量を減らす結合も使用しました。

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>();
于 2009-09-29T19:14:40.860 に答える
0

私は少し前に似たようなことをしなければなりませんでした。ニーズに合わせてこれを変更できると確信しています。

    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 を返すか、それを返す前に別の Expression を追加するかは、関数に任せてください。

于 2009-09-29T15:25:44.867 に答える