Get distinct month and year with SubSonic

949 views Asked by At

I have date type column (MySQL):

SELECT invdate FROM invoices;

invdate
-------
2009-08-22
2009-07-12
2009-08-23
-------

and I want get month and year like this:

SELECT DISTINCT MONTH(invdate) AS invmonth, YEAR(invdate) AS invyear FROM invoices;

how to use in C# with SubSonic (SimpleRepository)?

TIA

2

There are 2 answers

0
AJ. On BEST ANSWER

OMG! I forgot to define variable as date/datetime, so I can not use these functions.

before:

public string invdate { get; set; }

after:

public DateTime invdate { get; set; }

My mistake, I'm sorry. Problem solved.

0
LiamB On

Have you had a chance to have a look at the docs? See the link and code below, it should help.

http://subsonicproject.com/docs/Distinct

[Test]
public void SqlQuery_when_setting_distinct_it_should_set_IsDistinct()
{
    SubSonic.SqlQuery query= new 
        Select(Product.SupplierIDColumn).From<Product>().Distinct();
    Assert.IsTrue(query.IsDistinct);
}

[Test]
public void SqlQuery_should_handle_distinct()
{
    ProductCollection select = new 
        Select(Product.SupplierIDColumn).From<Product>().Distinct()
        .ExecuteAsCollection<ProductCollection>();

    Assert.AreEqual(29, select.Count);

}

[Test]
public void SqlQuery_GetRecordCount_should_handle_distinct()
{
    int select = new Select(Product.SupplierIDColumn)
        .From<Product>().Distinct()
        .GetRecordCount();

    Assert.AreEqual(29, select);
}