About

SubSonic is a .net open source project developed by Rob Conery and a core team of developers including Eric Kemp, Scott Watermasysk, Jon Galloway, Phil Haack, and Gavin Joyce. The current stable release is version 2.0.3. Nightly builds are available in our SVN respository.

Tags

Select Queries

Here are some sample Select queries using SubSonic 2.1. These come directly from our Unit tests, if you see an issue or a correction, please let us know!

All of these samples are made against the stock 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

            Product p = new Select().From<Product>().
               Where("ProductID").IsEqualTo(1).ExecuteSingle<Product>();
            Assert.IsNotNull(p);

Returning all columns

            int records = new Select().From("Products").GetRecordCount();
            Assert.IsTrue(records == 77);

Simple Where

            int records = new Select().From("Products").
                Where("categoryID").IsEqualTo(5).GetRecordCount();
            Assert.AreEqual(7, records);

Simple Where with And (as Collection)

            ProductCollection products =
                DB.Select().From("Products")
                    .Where("categoryID").IsEqualTo(5)
                    .And("productid").IsGreaterThan(50)
                    .ExecuteAsCollection<ProductCollection>();

 

Simple 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").IsEqualTo(5);

Multiple Joins As Collection

            CustomerCollection customersByCategory = new Select()
                .From(Customer.Schema)
                .InnerJoin(Order.Schema)
                .InnerJoin(OrderDetail.OrderIDColumn, Order.OrderIDColumn)
                .InnerJoin(Product.ProductIDColumn, OrderDetail.ProductIDColumn)
                .Where("CategoryID").IsEqualTo(5)
                .ExecuteAsCollection<CustomerCollection>();

Left Outer Join With Generics

            SubSonic.SqlQuery query = DB.Select(Aggregate.GroupBy("CompanyName"))
                .From<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").IsEqualTo(5).And("productid").IsGreaterThan(10)
                .OrExpression("categoryID").IsEqualTo(2).And("productID").IsBetweenAnd(2, 5)
                .ExecuteAsCollection<ProductCollection>();
            ProductCollection products = Select.AllColumnsFrom<Product>()
                .WhereExpression("categoryID").IsEqualTo(5).And("productid").IsGreaterThan(10)
                .Or("categoryID").IsEqualTo(2).AndExpression("productID").IsBetweenAnd(2, 5)
                .ExecuteAsCollection<ProductCollection>();

Simple Paged Query

            SubSonic.SqlQuery q = Select.AllColumnsFrom<Product>().
               Paged(1, 20).Where("productid").IsLessThan(100);

Paged 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

            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").IsEqualTo(5)
                )
                .GetRecordCount();

Using Multiple INs

            SubSonic.SqlQuery query = new Select()
                .From(Product.Schema)
                .Where(Product.CategoryIDColumn).In(2)
                .And(Product.SupplierIDColumn).In(3);
#1 mike on 7.11.2008 at 5:33 AM

What's the difference between these two?

new Select().From(OrderDetail.Schema)

new Select().From(Tables.OrderDetail)

#2 Rob Conery on 7.11.2008 at 1:45 PM

Nothing really - two different ways to do it is all. You can also use:

Select().From<OrderDetail>()

It's all the style you like.

#3 du8die on 7.11.2008 at 7:16 PM

How would you do something like...

select * from table

where column1 = 1 and (column2 = 2 or column2 = 3)

Thanks

#4 Rob Conery on 7.11.2008 at 8:11 PM

Good question :)

Select().From<Product>.Where(...)

.AndExpression(column2).IsEqualTo(2).Or(column2).IsEqualTo(3)

#5 Cedric on 7.12.2008 at 7:10 AM

Thanks, very helpful!

#6 nefajciar on 7.12.2008 at 8:15 AM

offtopic:

the following webcast isn't working on subsonic page: subsonicproject.com/.../webcast-using-s

any link for download?

thank you

#7 lalo on 7.15.2008 at 12:58 PM

Hi friends, i have a similar problem. I need to make this query:

select * from table where (a=value_a and (b=value_b or c=value_c))

Please any example of how to do it with Subsonic.

Thanks in advance!!

#8 Rob Conery on 7.15.2008 at 2:08 PM

lalo see comment 4

#9 Azure on 7.16.2008 at 10:51 PM

How would you do something like

select top 10 ....in single line code?

#10 Azure on 7.16.2008 at 10:54 PM

sorry..I found it.

top() is function..

Thx...

#11 Willie T on 7.17.2008 at 12:58 PM

Rob, maybe to alleviate some confusion, paste the Subsonic query syntax first and then what generic SQL it spits out

#12 CJ on 7.20.2008 at 6:08 AM

How would you use SubSonic for the following query:

SELECT whatever Customers

LEFT OUTER JOIN Orders

INNER JOIN OrderDetails

ON OrderDetails.OrderID = Orders.OrderId

ON Orders.CustomerID = Customers.CustomerID

Thanks,

CJ.

Subscribe