Search code examples
c#nhibernateunionnhibernate-criteria

Union with NHibernate and Criteria?


Union with NHibernate and Criteria:

Is it possible in Criteria or QueryOver? If not, is there any other way to achieve a union of two result within the same query?


Solution

  • You can't do a union directly, but you can do two future queries and union the results in code:

    var resultSet1 = this.Session.CreateCriteria<A>().Future<A>();
    var resultSet2 = this.Session.CreateCriteria<B>().Future<B>();
    

    After this, when either result set is enumerated, NHibernate will issue a single query to the database which will return multiple result sets. Note, if you are not using SQL Server, the database may not support multiple result sets.