我有这个查询
[HttpGet]
public List<AttachedPhotosModel> GetReportAttachedPhotos(int reportId)
{
var photos = new ReportsRepository().GetInjuryPhotos(reportId);
return photos.Select(x => new AttachedPhotosModel()
{
Id = x.Id,
Type = x.InjuryType,
Photos = photos.Where(y => y.InjuryType == x.InjuryType).Select(z => z.ServicePhotoUrl).ToList()
}).ToList();
}
我需要GroupBy InjuryType,怎么做?
我添加了return photos.GroupBy(k => k.InjuryType).Select(x => new AttachedPhotosModel()
,但如何选择模型,x
有新值key
,我不知道如何选择我的数据
答案 0 :(得分:0)
此代码应该有效。假设照片是具有InjuryType属性和PhotoUrl属性的对象的集合,而AttachedPhotosModel具有像这样的InjuryType和Photos属性。
public class AttachedPhotosModel
{
public string InjuryType { set; get; }
public List<string> Photos { set; get; }
}
按InjurType
进行分组的代码。
var grouped = photos
.GroupBy(s => s.InjuryType,d => d.PhotoUrl, (k, g) => new
AttachedPhotosModel
{
InjuryType = k,
Photos = g.ToList()
}).ToList();