LINQ group data into a list of objects 包含列表+选择特定的原始数据

LINQ group data into a list of objects containing a list + selecting specific original data

我正在搜索这个 Whosebug 问题中给出的解决方案的扩展。 Using Linq to group a list of objects into a new grouped list of list of objects

如果我想把一堆PersonData分到一个List里。如果我只想将 emp 字符串保留在 ListData 的标签内而不是点 Class.

中,我应该如何进行
return db.persondatas.Where.GroupBy(i => new { i.name, i.y })
                                    .Select(i => new Point
                                    {
                                        x = i.x
                                        emp = i.name
                                        y = i.Sum(s => s.y)
                                    })
                                    .GroupBy(i => i.emp)
                                    .Select(i => new ListData
                                    {
                                        label= i.Key,
                                        data= i.ToList()
                                    }).ToList();

public Class Point
{
  double x;
  double y;
}
public Class ListData
{
  List<Point> data;
  string label;
}
public Class PersonData
{
  string name;
  int x;
  int y;
}

你可以这样写:

return db.persondatas
    //.Where(/* ... */)
    .GroupBy(x => x.name, (name, g) => new ListData
    {
        label = name,
        data = g.GroupBy(d => d.x, (x, data) => new Point
            {
                x = x,
                y = data.Sum(d => d.y)
            })
            .ToList()
    }).ToList();