DataTable转Dictionary

DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("no");
dt.Columns.Add("age");
dt.Columns.Add("sex");
dt.Rows.Add(new object[] { "小张", "0001", 5, "男" });
dt.Rows.Add(new object[] { "小王", "0002", 6, "男" });
dt.Rows.Add(new object[] { "小李", "0003", 7, "女" });
Dictionary<string, string> dic = dt.Rows.Cast<DataRow>().ToDictionary(x => x[0].ToString(), x => x[1].ToString());
Dictionary<string, string> dic2 = dt.Rows.Cast<DataRow>().ToDictionary(x => x["name"].ToString(), x => x["sex"].ToString());
int i = 0;
Dictionary<string, int> dic3 = dt.Rows.Cast<DataRow>().ToDictionary(x => x["name"].ToString(), x =>i++);

posted @ 2019-01-15 16:00  逆光迷矢  阅读(2794)  评论(0编辑  收藏  举报