日期:2014-05-20  浏览次数:20767 次

请教一条linq查询语句。
现有数据结构如下
supplierCode time score
001 2012-09-01 70
001 2012-10-01 80
002 2012-09-01 75
002 2012-10-01 85

我希望能按供应商分组,然后获取他们的时间和分数。

期望查出的结果的例子:
supplierCode time score
001 string[] string[]
002 string[] string[]

或者将字符串相加也可以
supplierCode time score
001 "2012-09-01&2012-10-01" "70&80"
002 "2012-09-01&2012-10-01" "75&85"



------解决方案--------------------
DataTable dt = new DataTable();
dt.Columns.Add("supplierCode", typeof(string));
dt.Columns.Add("time", typeof(DateTime));
dt.Columns.Add("score", typeof(int));

@"001 2012-09-01 70
001 2012-10-01 80
002 2012-09-01 75
002 2012-10-01 85".Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(t => dt.Rows.Add(t.Split(' ')));
var source = from temp in dt.AsEnumerable()
group temp by temp.Field<string>("supplierCode").Trim() into g
select new
{
supplierCode = g.Key,
time = g.Select(t => t.Field<DateTime>("time").ToShortDateString()).ToArray(),
score = g.Select(t => t.Field<int>("score").ToString()).ToArray()
};