Icriteria = _ sessionfactory. getcurrentsession (). createcriteria (typeof (cardType ));
If (cardType. cardtypeid! = NULL)
{
Icriteria. Add (restrictions. Like ("cardtypename", "12% "));
}
If (cardType. cardtypeid! = NULL)
{
Icriteria. Add (restrictions. Like ("cardtypename", "12% "));
}
If (cardType. cardtypeid! = NULL)
{
Icriteria. Add (restrictions. Like ("cardtypename", "12% "));
}
If (cardType. cardtypeid! = NULL)
{
Icriteria. Add (restrictions. Like ("cardtypename", "12% "));
}
//
// String username = "name ";
// Var expr = predicatebuilder. True <user> ();
// If (! String. isnullorempty (namekeyword ))
//{
// Expr = expr. And (u => U. username. Contains (username ));
//}
VaR expr = predicatebuilder. True <cardType> ();
If (cardType. cardtypeid! = NULL)
{
Expr = expr. And (u => U. cardtypename = cardType. cardtypename );
}
_ Cardtyperepository. Query (). Where (expr );
This article describes the query and condition query, which may be based on SQL.ArticleIt is relatively simple. I just want to summarize the Nhibernate query methods, and most of them reference the library of Miss Li yongjing. If you want to see more details, please visit the blog of Miss Li yongjing.
First, the most basic query method of Nhibernate: Create an SQL statement using the createquery method of isession and return the corresponding data. Because it is an SQL statement, only a few queries are listed here, you can perform other operations on your own.
The following _ Session is consistent with queryhql. CS in entry 2.
Query all data of a customer: _ session. createquery ("from customer"). List <customer> ();
Return multiple objects or attributes/fields: Return _ Session. createquery ("select C. firstname, count (C. firstname) from customer C group by C. firstname "). list <object []> ();
Note that the returned list <object []>
Return a single field: Return _ session. createquery ("select C. customerid from customer C"). List <int> ();
Note the return type. Here, customerid is of the int type, so list <int> () is returned ();
Return Based on the condition:
There are three types of writing methods: the first one is understandable, but it is easy to inject. The second one is similar to the string. format in. net.
Code
Public Ilist < Customer > Getcustomersbyfirstname ( String Firstname)
{
// Statement 1, which will inject
// Return _ session. createquery ("from customer C where c. firstname = '" + firstname + "'")
// . List <customer> ();
//Statement 2: location parameters
//Return _ session. createquery ("from customer C where c. firstname =? ")
//. Setstring (0, firstname)
//. List <customer> ();
// Statement 3: naming parameters (recommended)
Return _ Session. createquery ( " From customer C where c. firstname =: FN " ). Setstring ( " FN " , Firstname). List < Customer > ();
} CopyCode
The above is the most basic query, which is consistent with SQL. I will not explain it here. The following is the criteria of Nhibernate to implement conditional query.
Criteria simply refers to returning data based on the expression you have set, or viewing the Code. This is intuitive:
Code
Public Ilist < Customer > Createcriteria ()
{
Icriteria crit = _ Session. createcriteria ( Typeof (Customer ));
Crit. setmaxresults ( 50 );
Ilist < Customer > MERs = Crit. List < Customer > ();
Return MERs MERS;
} Copy code
First, the createcriteria method of the isession interface is used to create a query instance for a specific persistence class of the nhib.pdf. icriteria interface.Typeof(Customer) is the Instance name, the second code is to set to return 50 data sets, can be understood as top 50.
Next, use add to add a constraint expression in the restrictions class. Restrictions provides a large number of common constraints, or intuitively says it is a condition:
Code
Public Ilist < Customer > Narrowing ()
{
Ilist < Customer > MERs = _ Session. createcriteria ( Typeof (Customer ))
. Add (restrictions. Like ( " Firstname " , " Yjing % " ))
. Add (restrictions. ( " Lastname " , " A % " , " Y % " ))
. List < Customer > ();
Return MERs MERS;
} Copy code
The above shows that like is a fuzzy query, between is the median value, and restrictions also contains more constraints, such as in/AND/OR =. You can perform this operation on your own.
Then, sort and use criterion.OrderSort, where true is ASC (ascending), false is desc (descending ):
Code
Public Ilist < Customer > Order ()
{
Return _ Session. createcriteria ( Typeof (Customer ))
. Add (restrictions. Like ( " Firstname " , " Y % " ))
. Addorder ( New Nhib.pdf. criterion. Order ( " Firstname " , False ))
. Addorder ( New Nhib.pdf. criterion. Order ( " Lastname " , True ))
. List < Customer > ();
} Copy code
There is also an example class that allows you to create query conditions based on the instance you specified, which can be understood as custom query conditions:
Code
Public Ilist < Customer > Query ()
{
Customer customersample = New Customer () {firstname = " Yjing " , Lastname = " Lee " };
Return _ Session. createcriteria ( Typeof (Customer ))
. Add (example. Create (customersample ))
. List < Customer > ();
} Copy code
Create a mermersample instance, use. Add (example. Create (customersample) to add it, and then return the object set according to example and set Nhibernate.
This article is extremely simple, because I personally think there is nothing to talk about, unless you have never learned T-SQL.
This is about queries. Next time we will talk about non-queries and transactions.Source codeThis is not the case.