NHibernate Tour (4): Explore the query criteria query (criteria query)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.