Series Introduction
The analysis series of nhibernate3.0 fully reveals new features, applications and various applications of nhibernate3.0 in terms of configuration, mapping, query, session policy, and application.ProgramBased on nhibernte3.0. If you are not familiar with Nhibernate, you can quickly get started with the Nhibernate series Article navigation series. If you are already using Nhibernate, keep up with the nhibernate3.0 series.
- Nhib.pdf topics: http://kb.cnblogs.com/zt/nhibernate/
- Nhibernate site: http://nhforge.org/
- Nhib.pdf Reference document: http://nhforge.org/doc/nh/en/
- Get Nhibernate address: http://sourceforge.net/projects/nhibernate/
Overview
Hql ast analyzer-based LINQ provider developed by Steve strong contributor and uses the third-party re-LINQ open-source framework at the underlying layer. Therefore, nhibernate3.0 has an additional required assembly: remotion. Data. LINQ. dll.
Note: In the previous versions of nhib.pdf, there was no LINQ function, ayende connector) (maintenance has been stopped). It is a LINQ provider based on the criteria API, the main function is to convert a simple LINQ expression to the criteria API. Due to the limited functionality of the Criteria API, there are many inherent limitations (the join and subquery functions are not supported ). If you use nhibernate2.1.0ga or nhibernate2.1.2ga, you can download and use nhiberante. LINQ. dll, which is not described here.
Next, let's take a look at the all-new nhib.pdf. LINQ query provided by nhib.pdf.
We use the query <t> () Extension Method of the isession interface to create a nhib.pdf. LINQ query.
Using nhib.pdf is required first. and then use isession. query <t> () to obtain iqueryable <t>. We perform some delayed operations (such as where and orderby) on it, and finally use non-delayed operations (such as tolist (), count (), firstordefault () returns the expected results.
Note: The Nhibernate. LINQ query converts the LINQ operator to hql. Some LINQ operators are specialized in processing sets, while SQL mainly processes unordered value sets. Therefore, the Nhibernate. LINQ query certainly does not need to support these operators that specifically process the set, such as except T, intersect, conversion operator, and generate operator.
The following lists all the LINQ operators and descriptions, and some simple Nhibernate. LINQ queries. I only operate on a single object user object:
// Code snippets copyright http://lyj.cnblogs.com/
Public classUser
{
PublicGuidId {Get;Set;}
Public StringName {Get;Set;}
Public intAge {Get;Set;}
}
Standard query operator 1. Basic Form
Use the isession query <user> () and then tolist () to query all user objects.
// Code snippets copyright http://lyj.cnblogs.com/
VaRBasicquery = (FromUserInSession. query <User> ()
SelectUser). tolist ();
VaRBasicquery2 = session. query <User> (). Tolist ();
2. Restricted Operators
- Where: filtering items in a sequence
Description: filters attribute values. Filters attributes by component, enumeration, association, and basic type. For example, int type equals, greater than, less than, not equal to; string type startswith, endswith, contains, equals, tolower, tolowerinvariant, toupper, toupperinvariant, substring, indexof, etc; datetime year, date, month, and so on. You can also use the subquery of the aggregation operator in the where operator.
// Code snippets copyright http://lyj.cnblogs.com/
VaRRestrictionquery = (FromUserInSession. query <User> ()
WhereUser. Name ="Li yongjing"
SelectUser). tolist ();
VaRRestrictionquery2 = session. query <User> (). Where (O => O. Name ="Li yongjing"). Tolist ();
3. projection operators
- Select: Create projection of Partial Sequences
- Selectprojection: creates one-to-multiple projection of some sequences.
Note: The select operator supports most operations and subqueries, but does not support nested select statements. Selectworkflow is not supported.
// Code snippets copyright http://lyj.cnblogs.com/
VaRSelectanonymousquery = (FromUserInSession. query <User> ()
Select New{User. Name, age = user. Age}). tolist ();
VaRSelectanonymousquery2 = session. query <User> ()
. Select (O =>New{O. Name, age = O. Age })
. Tolist ();
4. Partition Operators
- SKIP: the sequence of skipping a specified number of items is returned.
- Skipwhile: Return to skip the sequence that does not meet the expression item
- Take: returns a sequence with a specified number of items.
- Takewhile: returns a sequence with a project that meets the expression
Note: skipwhile and takewhile are not supported. Multiple take or skip addresses cannot be connected.
// Code snippets copyright http://lyj.cnblogs.com/
VaRPartitioningquery = (FromUserInSession. query <User> ()
SelectUser). Take (2). Skip (2). tolist ();
VaRPartitioningquery2 = session. query <User> (). Take (2). Skip (2). tolist ();
5. Sorting Operators
- Orderby: sort order by value in ascending order
- Orderbydescending: Order by value in descending order
- Thenby: Sort sorted sequences in ascending order
- Thenbydescending: Sort sorted sequences in descending order
- Reverse: reverse sequence of items in a sequence (used to operate a set)
Note: Sorting operators do not support subqueries.
// Code snippets copyright http://lyj.cnblogs.com/
VaROrderingquery = (FromUserInSession. query <User> ()
OrderbyUser. IDDescending, User. NameAscending
SelectUser). tolist ();
VaROrderingquery2 = session. query <User> ()
. Orderbydescending (O => O. ID). orderby (O => O. Name). tolist ();
6. grouping operators
For example, the following query:
// Code snippets copyright http://lyj.cnblogs.com/
VaRGroupquery = (FromUserInSession. query <User> ()
GroupUserByUser. Name
IntoG
Select New
{
G. Key,
Age = G. sum (P => P. Age)
}). Tolist ();
VaRGroupquery2 = session. query <User> (). Groupby (O => O. Name)
. Select (O =>New{O. Key, age = O. sum (P => P. Age)}). tolist ();
7. Set Operators
- Distinct: returns a sequence of non-repeated projects.
- Distinct T: returns the sequence representing two sequence difference sets (used to operate the set)
- Intersect: return the sequence representing the intersection of two sequences (used to operate the Set)
- Union: returns a sequence that represents the intersection of two sequences (for operation sets)
Currently, distinct is supported:
// Code snippets copyright http://lyj.cnblogs.com/
VaRDistinctquery = session. query <User> (). Distinct (). tolist ();
8. Conversion operators (used to operate a set)
- Cast: converts an element in a sequence to a specified type.
- Oftype: filter elements of the specified type in the sequence.
- Toarray: returns an array from the sequence.
- Todictionary: returns a dictionary from the sequence.
- Tolist: returns a list from the sequence.
- Tolookup: returns a query from the sequence.
- Tosequence: returns an ienumerable sequence.
Nhib.pdf. LINQ is not required.
9. Element Operators
- Defaultifempty: Creates a default element for an empty sequence (used to operate a set)
- Elementat: returns the elements of the specified index in the sequence (used to operate the set)
- Elementatordefault: return the element of the specified index in the sequence, or if the index exceeds the range, return the default value (used to operate the set)
- First: returns the first element in the sequence.
- Firstordefault: returns the first element in the sequence, or returns the default value if no element is found.
- Last: returns the last element in the sequence (used to operate the set)
- Lastordefault: returns the last element in the sequence, or returns the default value (used to operate the Set) If no element is found)
- Single: returns a single element in the sequence.
- Singleordefault: returns a single element in the sequence, or returns the default value if no element is found.
For example:
// Code snippets copyright http://lyj.cnblogs.com/
VaRFirstquery = session. query <User> (). First (u => U. Name ="Li yongjing");
VaRFirstordefaultquery = session. query <User> (). Firstordefault (u => U. Name ="Li yongjing");
VaRSinglequery = session. query <User> (). Single (u => U. Name ="Li yongjing");
VaRSingleordefaultquery = session. query <User> (). Singleordefault (u => U. Name ="Li yongjing");
10. generate an operator (used to operate a set)
- Empty: generate an empty Sequence
- Range: generate a sequence of specified ranges.
- Repeat: generates a sequence by repeating a specified number of times.
Nhib.pdf. LINQ is not required.
11. Qualifier
- ALL: determines whether all items in the sequence meet a certain condition.
- Any: determines whether any project in the sequence meets the conditions
- Contains: determines whether the sequence contains the specified project
Write down only any example:
// Code snippets copyright http://lyj.cnblogs.com/
VaRAnyquery = session. query <User> (). Any ();// Obtain any
12. Aggregate Operators
- Aggregate: executes a custom method for the sequence.
- Average: calculates the average value of a numerical sequence.
- Count: return the number of items in the sequence (integer)
- Longcount: number of items in the returned sequence (long type)
- Min: Find the minimum number in the Number Sequence
- MAX: Find the maximum number in the Number Sequence
- Sum: number in the summary Sequence
Some simple examples:
// Code snippets copyright http://lyj.cnblogs.com/
VaR Aggresponquery = session. query < User > ()
. Where (O => O. Name. Contains ( "Li yongjing" ))
. Select (O => O. ID)
. Aggregate ( New Stringbuilder (), (SB, ID) => Sb. append (ID). append ( "," ));
VaR Average = session. query < User > (). Average (u => U. Age );
VaR Countquery = session. query < User > (). Count ();
VaR Longcountquery = session. query < User > (). Longcount ();
VaR Minquery = session. query < User > (). Min (u => U. Age );
VaR Maxquery = session. query < User > (). Max (u => U. Age );
VaR Sumquery = session. query < User > (). Sum (u => U. Age );
13. Join Operators
- Concat: concatenates two sequences into one.
Not Supported yet!
14. Join Operators
- Groupjoin: Join two sequences by grouping
- Join: joins two sequences from the interior.
Related Object operations will be introduced separately later.
Conclusion
This article uses a single object to learn the most basic standard query of nhib.pdf. LINQ. It does not involve multiple object operations and is relatively simple. It aims to familiarize you with nhib.pdf. LINQ queries first. Next, we will continue to learn about nhib.pdf. LINQ. Mainly include nhib.pdf. LINQ complex standard query, nhib.pdf. LINQ enhanced query, custom nhib.pdf. LINQ query and Its Application in the project.
I hope this article will help you.
Tags: Nhib1_nhibernate3.0 nhib1_query nhib1_. LINQ