How to create a dynamic query with SubSonic

I need to build (and run) a query based on different conditions. When I was creating a SQL literal to run, it would mean concatenating strings to dynamically create SQL.

How do I do this with SubSonic? If, for example, I need to add a WHERE clause in some case and ORDER BY in other cases. What would the code look like to create and run this request?

0


a source to share


1 answer


Here's an example with v2.1 + and BaseClass set to RepositoryRecord:



var q = DB.Select().From<Product>();

if (someCondition) 
    q.Where(Product.ProductIdColumn).IsEqualTo(1);

if (order == "ASC")
    q.OrderAsc(Product.Columns.ProductId)
else
    q.OrderDesc(Product.Columns.ProductId)

var results = q.ExecuteAsCollection<ProductCollection>();

      

+3


a source







All Articles