I'm developing purchase order search function using Dapper.NET
. User can search by invoice no, and purchase date.
If user fill the invoice no on textbox, it will search by invoice no,
if user fill the invoice no and purchase date, it will search by invoice no and purchase date, and
if user fill the purchase date, it will search by purchase date
So, the query:
string query = "SELECT PurchaseDate, InvoiceNo, Supplier, Total FROM PurchaseOrder WHERE 1 = 1";
if (!string.IsNullOrEmpty(purchaseOrder.InvoiceNo))
{
query += " AND InvoiceNo = @InvoiceNo";
}
if (purchaseOrder.PurchaseDate != DateTime.MinValue)
{
query += " AND PurchaseDate = @PurchaseDate";
}
return this._db.Query<PurchaseOrder>(sql, ?).ToList();
The problem is I don't know how to pass the values dynamically based on number of criteria in the query.