经常拼凑sql语句,经常是手工拼写 也可以利用字典另外一个模式拼凑
这里采用的是Dictionary中的Aggregate方法。
代码如下:
static void Main(string[] args) { var dict = new Dictionary<string, string>(); dict["username"] = "Yuhao"; dict["password"] = "abc@123"; var sq = dict.Aggregate("select * from users where 1=1", (current, param) => Addparam(current, param.Key, param.Value)); Console.WriteLine(sq); Console.Read(); } private static string Addparam(string rawValue, string key, string value) { return rawValue = rawValue + " and " + key + "='" + value + "'"; }
感谢群友的帮助