0

I am very new to Nhibernate, so this may sound silly.

I have a function which receives parameters based on what I need to make queries:

Public List<Data> GetResultData(SearchParams[] searchparams) {}

In this case, SearchParams is my class which has few properties say ID and Name which will be used to query.

So here are two challenges I am facing:

  1. I need to make a query based only on the property of SearchParams which are filed. Say one object of searchParams array is like this:

    ID,  NAME 
    "34", ""
    

    As we can see the NAME doesn't have value. So my query needs to be like:

    select * from DB where ID ="34".
    

    The query has to be created at runtime on the basis of values available. If both properties are available, then it will be AND of both.

  2. Since the input is an array of search parameters, there will be as many queries as the number of objects. I am trying to think of a way in which I can make a UNION of all these queries and fire them in one go.

Thoughts?

Jeff B
  • 8,572
  • 17
  • 61
  • 140
Gurinder Raj
  • 151
  • 1
  • 6

1 Answers1

-1

A sequence of restrictions linked with or is called a disjunction. A sequence of restrictions linked with and is called a conjunction.

So, what you need is

one conjunction for your third condition one disjunction to link the first, second, and third conditions: So here it goes:

Criterion startInRange = Restrictions.between("expectedStartCanonicDate", rangeStart, rangeEnd);

Criterion endInRange = Restrictions.between("expectedCompletionCanonicDate", rangeStart, rangeEnd);

Criterion thirdCondition = 
    Restrictions.conjunction().add(Restrictions.le("expectedStartCanonicDate", rangeStart))
                              .add(Restrictions.ge("expectedCompletionCanonicDate", rangeEnd));

Criterion completeCondition = 
    Restrictions.disjunction().add(startInRange)
                              .add(endInRange)
                              .add(thirdCondition);

criteria.add(completeCondition);
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
Gurinder Raj
  • 151
  • 1
  • 6