Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
var l = new List<string>();
if (condition1) l.Add("CONDITION1 = ? ");
if (condition2) l.Add("CONDITION2 = ? ");
if (condition3) l.Add("CONDITION3 = ? ");
string sql = "SELECT * FROM table_1 WHERE " + string.Join(" AND ", l.ToArray());
content = elem[0];
while not end {
content += ', ' + elem[nextNdx]
}
StringBuilder sb = new StringBuilder("1");
sb.Append("2");
string s = sb.ToString();
string s = "1" + "2";
new [] { } .Where(p => p != null).Aggregate((f, s) => f + ', ' + s);var filters = new[]{
new FuncTuple(() => true, "sdf"),
new FuncTuple(() => false, "sdfdfgdfg"),
new FuncTuple(() => true, "sddfgdfgf")
};
var s = string.Format("dfsdfsdfsd {0}", string.Join(", ", from x in filters where x.Item1() select x.Item2));
class FilterList<TResult>:IEnumerable<TResult>
{
private readonly List<Tuple<Func<bool>,TResult>> _filters = new List<Tuple<Func<bool>,TResult>>();
public void Add(Func<bool> predicate, TResult value)
{
_filters.Add(Tuple.Create(predicate, value));
}
public IEnumerator<TResult> GetEnumerator()
{
return (from x in _filters where x.Item1() select x.Item2).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
var t = new FilterList<string>
{
{()=>true, "fdgdgfg"},
{()=>false, "fdgsdfsdfdgfg"},
{()=>true, "fdgfhdhfdhgdgfg"},
};
var s = string.Join(", ", t);
string BuildSQL(string arg1, string arg2, string arg3)
{
string sql = "SELECT * FROM table_1 WHERE 1=1 ";
if (arg1 != null) sql += "AND ARG1 = ? ";
if (arg2 != null) sql += "AND ARG1 = ? ";
if (arg3 != null) sql += "AND ARG1 = ? ";
return sql;
}
Миникостыли: склеиваем строку из фрагментов