views:

248

answers:

3

I'd like to achieve the following SQL statement with subsonic 2.2

SELECT Product.* FROM Product WHERE Product.OurPrice <> Product.RetailPrice

The Subsonic select query I've started with:

SubSonic.SqlQuery select = new SubSonic.Select()
.From<Product>()
.Where(Product.Columns.OurPrice)
.IsNotEqualTo(... object /*Should be Product.Columns.RetailPrice, but that's giving and exception*/...);

My question is how to tell SubSonic 2.2 to generate a where condition against another column in the same table.

A: 

It looks like you're trying to compare two columns within your query. This isn't possible in SubSonic 2.2 unless you use an inline query:

http://subsonicproject.com/docs/Inline_Query_Tool

You could also possibly use a view or table based function.

Adam
The inline query was working. It's a pity there is no other option only pure SQL. At least it's working! Thanks.
csizo
A: 

The solution for the problem above:

ProductCollection products = new SubSonic.InlineQuery().ExecuteAsCollection<ProductCollection>
(@"SELECT Product.ProductId, ... Product.ModifiedBy, Product.ModifiedOn, Product.IsDeleted FROM Product WHERE (Product.OurPrice <> Product.RetailPrice)");

Repeater1.DataSource = products;
Repeater1.DataBind();
csizo
A: 

You can use the old Query object (still included in 2.2) like so:

Query q = new Query(Product.Schema.TableName).WHERE("OurPrice <> RetailPrice");
ProductCollection products = new ProductCollection();
products.LoadAndCloseReader(q.ExecuteReader());
John Sheehan
Where returns a Constraint not a SqlQuery so that code won't work unfortunately.
Adam
Sorry, I was thinking of the old Query object. I've updated my answer with the correct example
John Sheehan