Linq Group by分组

1:先添加一个classOutput.cs类

public string Name { get; set; }
public List<TemplateList> TemplateLists;
public class TemplateList
{
public int Id { get; set; }
public string Name { get; set; } 
}

2

  var result = (from m in this.Context.ClassOutput
                          group m by m.Name into lists
                          select new TemplateOutput
                          {
                              TemplateName = lists.Key,
                              TemplateLists = (from n in lists
                                                            select new TemplateList
                                                            {
                                                                Id = n.Id,
                                                                TemplateName = n.TemplateName, 
                                                            }).ToList()
                          }
                          );

  

原文地址:https://www.cnblogs.com/BkyeSky/p/12018848.html