C# - Dictionary join keys or join Values
2016-03-07 12:32
435 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class Program { public static void Main() { string strID = "10010"; Dictionary<string, string> g = new Dictionary<string, string>(); g.Add("K", "k1"); g.Add("L", "l1"); string updateCustomerIncome = string.Format(" update CustomerIncome set {0} where ID = '{1}'; ", string.Join(", ", g.Select(x => String.Format("{0} = '{1}'", x.Key, x.Value))), strID); Console.WriteLine(updateCustomerIncome); //var result2 = string.Join(", ", g.Select(x => String.Format("{0} = '{1}'", x.Key, x.Value))); //Console.WriteLine(result2); //Console.WriteLine(string.Join(", ", new List<string>(g.Keys))); //Console.WriteLine(string.Join(", ", new List<string>(g.Values))); string insertCustomerIncome = string.Format(" insert CustomerIncome(ID, {0}) values ('{1}', '{2}');", string.Join(", ", new List<string>(g.Keys)), strID, string.Join("', '", new List<string>(g.Values))); Console.WriteLine(insertCustomerIncome); StringBuilder sb = new StringBuilder(); sb.AppendFormat("if exists (select * from CustomerIncome where id = '{0}')", strID); sb.AppendLine(); sb.AppendLine(updateCustomerIncome); sb.AppendLine("else"); sb.Append(insertCustomerIncome); Console.WriteLine(sb.ToString()); } }
the output
update CustomerIncome set K = 'k1', L = 'l1' where ID = '10010'; insert CustomerIncome(ID, K, L) values ('10010', 'k1', 'l1'); if exists (select * from CustomerIncome where id = '10010') update CustomerIncome set K = 'k1', L = 'l1' where ID = '10010'; else insert CustomerIncome(ID, K, L) values ('10010', 'k1', 'l1');
references:
https://dotnetfiddle.net/uiFWXo
How to get the list of key in dictionary
相关文章推荐
- 关于PROPAGATION_REQUIRES_NEW和PROPAGATION_NESTED事务的比较
- lcs hdu1503 Advanced Fruits
- iOS集成环信聊天和EaseUI之后出现警告:Warning! ivar size mismatch in PSUICollectionView_ - can't change the supercl
- iOS键盘类型UIKeyboardType(模拟器效果+真机效果)
- AsyncQueryHandler 异步查询数据
- 【前端】require函数实现原理
- Parquet 写数据过程及源代码分析
- js生成uuid的方法
- iOS对UIViewController生命周期和属性方法的解析
- A request has been denied as a potential CSRF attack.”
- Map.values方法——获取Map集合中的所有键值对象
- mysql怎样利用sql语句对指定列添加unique属性
- SeaJS与RequireJS最大的区别
- IOS开发之修改UISearchBar的背景颜色
- UIView转成UIImage
- Reason: no suitable image found. Did find:
- 关于UITableView的cell复用问题的3种解决方法
- Command and Query Responsibility Segregation (CQRS) Pattern 命令和查询职责分离(CQRS)模式
- 108.Which two statements are true regarding the COUNT function? (Choose two.)
- DXXcodeConsoleUnicodePlugin debug栏打印时自动把/ueo6转化成汉字