ado.net - DbExtensions - How to create WHERE clause with OR conditions? -
i'm trying create where
clause or
conditions using dbextensions.
i'm trying generate sql statement looks like
select id, name employee id = 100 or name = 'test'
my c# code is
var sql = sql.select("id, firstname") .from("employee") .where("id = {0}", 10) .where("name = {0}", "test");
how or seperator using above mentioned dbextensions library?
i have found definition logical or operator here
public sqlbuilder _or<t>(ienumerable<t> items, string itemformat, func<t, object[]> parametersfactory) { return _foreach(items, "({0})", itemformat, " or ", parametersfactory); }
and code examples here
public sqlbuilder or() { int[][] parameters = { new[] { 1, 2 }, new[] { 3, 4} }; return sql .select("p.productid, p.productname") .from("products p") .where() ._or(parameters, "(p.categoryid = {0} , p.supplierid = {1})", p => new object[] { p[0], p[1] }) .order_by("p.productname, p.productid desc"); }
i think (by analogy example) in case code should (but can't test sure):
var params = new string[] { "test" }; var sql = sql.select("id, firstname") .from("employee") .where("id = {0}", 10) ._or(params, "name = {0}", p => new object[] { p })
hope helps :)
by way... have tried way?
var sql = sql.select("id, firstname") .from("employee") .where(string.format("id = {0} or name = '{1}'", 10, "test"))
Comments
Post a Comment