请教一条linq查询语句。

TLJewel 2012-09-05 02:00:21
现有数据结构如下
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"

...全文
40 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
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()
};

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧