Go to subsonic queries query help

Source: Internet
Author: User

Currently, subsonic is often used in a small project to collect common methods for reference.

The following is fromSubsonicExamples on the official website are based on the northwind database

Simple select with string Columns

 
Int records = new select ("productid"). From ("Products"). getrecordcount (); Assert. istrue (records = 77 );

Simple select with typed Columns

 
Int records = new select (product. productidcolumn, product. productnamecolumn). From <product> (). getrecordcount (); Assert. istrue (records = 77 );

Returning a single object (returns a simple object)

 
Product P = new select (). from <product> (). where ("productid "). isdue to (1 ). executesingle <product> (); assert. isnotnull (P );

Returning all columns (returns all columns)

 
Int records = new select (). From ("Products"). getrecordcount (); Assert. istrue (records = 77 );

Simple where (simple where Statement)

Int records = new select (). From ("Products"). Where ("categoryid"). ispartition to (5). getrecordcount (); Assert. areequal (7, records );

Simple where with and (as collection) (where statement with and, returns a set)

 
Productcollection products = dB. select (). from ("Products "). where ("categoryid "). isdue to (5 ). and ("productid "). isgreaterthan (50 ). executeascollection <productcollection> ();

Simple inner join (inner join)

Subsonic. sqlquery q = new select ("productid"). From (orderdetail. schema). innerjoin (product. schema). Where ("categoryid"). isequalto (5 );

Simple join with table Enum

 
Subsonic. sqlquery q = new select (). From (tables. orderdetail). innerjoin (tables. Product). Where ("categoryid"). iseconto (5 );

Multiple joins as collection (Multi-Level join)

Customercollection customersbycategory = new select (). from (customer. schema ). innerjoin (Order. schema ). innerjoin (orderdetail. orderidcolumn, order. orderidcolumn ). innerjoin (product. productidcolumn, orderdetail. productidcolumn ). where ("categoryid "). isdue to (5 ). executeascollection <customercollection> ();

Left Outer Join with generics (left Outer Join)

Subsonic. sqlquery query = dB. Select (aggregate. groupby ("companyName"). From & lt; Customer> (). leftouterjoin <order> ();

Left Outer Join with Schema ()

 
Subsonic. sqlquery query = dB. Select (aggregate. groupby ("companyName"). From (customer. schema). leftouterjoin (order. customeridcolumn, customer. customeridcolumn );

Left Outer Join with magic strings

Subsonic. sqlquery query = dB. Select (aggregate. groupby ("companyName"). From ("customers"). leftouterjoin ("orders ");

Simple select with collection result

 
Productcollection P = select. allcolumnsfrom <product> (). executeascollection <productcollection> ();

Simple select with like

Productcollection P = dB. select (). from (product. schema ). innerjoin (category. schema ). where ("categoryname "). like ("C % "). executeascollection <productcollection> ();

Using nested where/and/or

 
Productcollection products = select. allcolumnsfrom <product> (). whereexpression ("categoryid "). isdue to (5 ). and ("productid "). isgreaterthan (10 ). orexpression ("categoryid "). isdue to (2 ). and ("productid "). isbetweenand (2, 5 ). executeascollection <productcollection> ();
Productcollection products = select. allcolumnsfrom <product> (). whereexpression ("categoryid "). isdue to (5 ). and ("productid "). isgreaterthan (10 ). or ("categoryid "). isdue to (2 ). andexpression ("productid "). isbetweenand (2, 5 ). executeascollection <productcollection> ();

Simple paged query (paging query)

 
Subsonic. sqlquery q = select. allcolumnsfrom <product> (). Paged (1, 20). Where ("productid"). islessthan (100 );

Paged query with join (paging query with join)

Subsonic. sqlquery q = new select ("productid", "productname", "categoryname "). from ("Products "). innerjoin (category. schema ). paged (1, 20 );

Paged View

 
Subsonic. sqlquery q = new select (). From (invoice. schema). Paged (1, 20 );

Simple in query (in query)

Int records = new select (). from (product. schema ). where ("productid "). in (1, 2, 3, 4, 5 ). getrecordcount (); assert. istrue (records = 5 );

Using in with nested select

 
Int records = select. allcolumnsfrom <product> (). where ("productid "). in (New select ("productid "). from (product. schema ). where ("categoryid "). isdue to (5 )). getrecordcount (); (number of returned Records)

Using multiple INS

Subsonic. sqlquery query = new select (). from (product. schema ). where (product. categoryidcolumn ). in (2 ). and (product. supplieridcolumn ). in (3 );
 
Select * from table where column1 = 1 and (column2 = 2 or column2 = 3) to select (). from <product>. where (...). andexpression (column2 ). isdue to (2 ). or (column2 ). for more information about subsonic, seeHttp://subsonicproject.com/Official website.
Related Article

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.