Summary
This article introduces query over queries introduced in NHibernate 3.2, which is similar to the criteria query. The Iqueryover object is created first, and then the object is queried by invoking the object's API function. This article uses query over to rewrite all previous queries.
The code of this article can be downloaded to nhibernate query
1. Create Iqueryover object, return all customer information
1 Public Ilist<customer> queryallover ()2 {3 return Session. Queryover<Customer>(). List (); 4 }
2. Specify object, return array
1 Public ilist<int> selectidover ()2 {3 return Session.queryover<customer>()4 . List<customer> (). Distinct (). Select (c = c.id). ToList (); 5 }
3. Add Query criteria
1 Public Ilist<customer> getcustomerbynameover (stringstring lastName)2 {3 return session.queryover<customer> (). Where(c = c.firstname = = FirstName && C.lastname = = LastName). List (); 4 }
Another example of a fuzzy query
1 PublicIlist<customer>Getcustomersstartwithover ()2 {3 //return session.queryover<customer> (). Where (c = c.firstname.startswith ("J")). List (); //Exception4 //return session.queryover<customer> (). Where (c = = C.firstname = = "j%"). List (); //correct5 //return session.queryover<customer> (). Where (restrictions.on<customer> (c = c.firstname). Islike ("j%")). List (); //correct6 returnSession.queryover<customer> (). Where (Restrictions.like ("FirstName","j%")). List ();7}
The first sentence above will be reported as abnormal. Use query over to not invoke all. NET self-bringing class objects in the Lamda parameter, where a method called the string class throws an exception. You cannot access the collection properties of a NHibernate entity object, for example, you cannot access C in an LAMDA expression. Orders.
return Session.queryover<customer> (). Where (c = c.firstname.startswith ("J")). List (); // Exception
4. ORDER BY
1 Public Ilist<customer> getcustomersorderbyover ()2 {3 return session.queryover<customer> (). (c = c.firstname). Desc (). List (); 4 }
Sort multiple fields after the first method call, call the ThenBy method.
5. Related queries
1 PublicIlist<ordercount>Selectordercountover ()2 {3 varquery = session.queryover<customer>()4. Joinqueryover<demo.xml.entities.domain.order> (o = o.orders)//Association query, associated with order object, default is inner join, can call overloaded method to specify join mode5. Select (Projections.groupproperty ("Id"), Projections.rowcount ())//Group Queries6. Transformusing (transformers.aliastobean<ordercount> ());//projecting the results to a OrderCount object7 returnQuery. List<ordercount>();8 }9 Ten /// <summary> One ///query customer information for all orders with a quantity greater than 2 A /// </summary> - /// <returns></returns> - PublicIlist<customer>Getcustomersordercountgreaterthanover () the { - //Group Queries - varquery = session.queryover<customer>() -. Joinqueryover<demo.xml.entities.domain.order> (o =o.orders) +. Select (Projections.groupproperty ("Id"), Projections.rowcount ()); -ilist<Object[]> groups = query. list<Object[]>(); + //get the number of orders greater than 2 customer.id Ailist<int> ids = Groups. Where (g = (int) g[1] >2). Select (g = (int) g[0]). ToList (); at //Conditional Query - returnSession.queryover<customer>() -. Where (Restrictions.in ("Id"Ids. toarray<int>())) -. List<customer>(); - } - in /// <summary> - ///query customer information for placing an order within the current time specified date to /// </summary> + /// <param name= "OrderDate" ></param> - /// <returns></returns> the PublicIlist<customer>getcustomersorderdategreatthanover (DateTime orderDate) * { $ varquery = session.queryover<customer>()Panax Notoginseng. Joinqueryover<demo.xml.entities.domain.order> (o = o.orders)//Correlation Query -. Where (o = o.ordered > OrderDate);//Query Criteria the returnQuery. List<customer>(); +}
Conclusion
NHibernate Query over is similar to criteria, is not very flexible, and the API is limited, we just need to understand the basic usage. The next article describes the NHibernate query that another person uses very much: Native queries.
NHibernate Series Article 25: NHibernate query over query (attached program download)