ユーザーが指定したパラメーターに基づいて注文のリストを取得しようとしています (基本的な検索機能)。ユーザーは 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;
}
}