3
\$\begingroup\$

This code will retrieve all records from the database if the id is null or return records by the available id. Is there a way to combine the query instead of having two separate queries?

// retrieve standard orders
let retrieveStdOrders id = 

    if id = null then
        query{
            for row in tool.db.Order_Order_StandardOrder do
            select {
                Id = row.Id;
                BusinessLocationId = row.BusinessLocationId;
                ProductOptionId = row.ProductOptionId;

            }
        }
    else
        query{
            for row in tool.db.Order_Order_StandardOrder do
            where(row.Id = id)
            select {
                Id = row.Id;
                BusinessLocationId = row.BusinessLocationId;
                ProductOptionId = row.ProductOptionId;

            }
        }
\$\endgroup\$
2
  • \$\begingroup\$ Have a look at F# query expressions and composability. \$\endgroup\$
    – svick
    Jun 18, 2014 at 18:20
  • \$\begingroup\$ If your function accepts an id value that could also be a non-value, you should rather use an option type: let retrieveStdOrders id = match id with Some id -> (* do something with the id *) | None -> (* do something without an id *) or even even shorter: let retrieveStdOrders id = function Some id -> (* ... *) | None -> (* ... *). \$\endgroup\$
    – Yaar Hever
    Mar 14, 2016 at 19:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.