我有一个自定义类工作日的列表:
class workdate
{
string dayid;
List<string> blocks;
}
Workdates.Add(new workdate("1", {"a","b","c"});
Workdates.Add(new workdate("2", {"a","b"});
Workdates.Add(new workdate("1", {"d","e","f"});
如何使用linq基于相同的日期合并列表项:
&#34; 1&#34;,{&#34; a&#34;,&#34; b&#34;,&#34; c&#34;,&#34; d&#34;,& #34; E&#34;&#34; F&#34;}
&#34; 2&#34;,{&#34; a&#34;,&#34; b&#34;}
答案 0 :(得分:3)
Workdates.GroupBy(x => x.dayid)
.Select(g => new workdate
{
dayid = g.Key,
blocks = g.SelectMany(x => x.blocks).ToList()
}).ToList())