The contents of this section
The Query method in NHibernate
Conditions query (criteria query)
1. Create a Icriteria instance
2. Result set limits
3. Result set Ordering
4. Some notes
Based on the sample query (query by Example)
Example analysis
Conclusion
In the previous section, we introduced one of the NHibernate query languages: The NHibernate Query Language (hql,nhibernate queries Language), which describes the conditional query (criteria API).
The Query method in NHibernate
There are three types of queries available in NHibernate: NHibernate Query Language (hql,nhibernate query Language), conditional query (criteria Api,criteria query), (based on sample queries (Qbe,query by Example) is a special case of conditional queries, native SQL (Literal sql,t-sql, Pl/sql). Everyone has different preferences and expertise, can choose to use one or several of them according to their own circumstances. In this section we introduce the conditional query.
Conditions query (criteria query)
HQL is extremely powerful, but some people want to be able to dynamically use an object-oriented API to create queries instead of. NET code to embed the string. In NHibernate, an intuitive, extensible criteria API is provided. When we type query statements, we provide a compile-time syntax check, VS provides a powerful smart tip. It may be easier to use this method if you don't feel comfortable with HQL's grammar. This API is also more scalable than HQL.
Typical usage: Create a Icriteria instance object from the ISession interface, set one or more expressions on the Icriteria instance object, require the Icriteria interface to return the required list, or return an object from the database based on an expression.
Note: Due to its limited space, I just posted the code for the data access layer here. The code to test these methods is not posted, and you can download the source code for this series to take a closer look at the codes that test these methods. These examples I strive to write to be able to run up, we download the source to see the effect, some data needs to be changed according to the data of the personal database. For example, query conditions and results. This section, based on the previous section of the source code, creates a new QueryCriteriaAPI.cs class in the data access layer to write the conditional query method and create a new QueryCriteriaAPIFixture.cs class for testing at the test layer of the data access.