例如有以下List<Model>定义
List<AModel> AModelList= new List<AModel>();
public class AModel
{
public string ID {get; set;}
public string Name {get; set;}
}
如果需要按照ID 进行分组则需要定义如下List<Model>
List<BModel> BModelList= new List<BModel>();
public class BModel
{
public string ID {get; set;}
public List<AModel> TS {get; set;}
}
GroupBy语句如下:
var GroupList = AModelList.GroupBy(x => x.ID).Select(x => new BModel{ ID= x.Key, TS = x.ToList() });