var query = from y in (from x in DataTable.Cast<DataRow>()
select new { a = x["列1"].ToString(), b = x["列2"].ToString() })
group y by y.a into g
select new { key = g.Key, values = g.Select(z => z.b).ToArray() };
foreach (var item in query)
{
string name = item.key;
string[] values = item.values;
...//你可以处理,已经归类放入数组了
}
------解决方案--------------------
------解决方案-------------------- dim list as new dictionary(of string,list(of string))
循环数据库数据 if list.ContainsKey("列1") then list("列1").add("列2") else list("列1")=new list(of string) list("列1").add("列2") end
------解决方案-------------------- 如果你要转换成字典,用linq还是一行。
C# code
var query = from y in (from x in DataTable.Cast<DataRow>()
select new { a = x["列1"].ToString(), b = x["列2"].ToString() })
group y by y.a into g
select new { key = g.Key, values = g.Select(z => z.b).ToArray() };
Dictionary<string, List<string>> list = query.ToDictionary(x => x.key, x => x.values.ToList());
------解决方案--------------------
------解决方案-------------------- var source = from temp in dt.AsEnumerable() group temp by temp.Field<string>("列1") into g select new { key = g.Key, g, values = g.Select(t => t.Field<string>("列2")).ToArray() }; foreach (var t in source) { string[] str= t.values; }