0
我有一组存储在对象ReportModel像下面的数据:C#字典从映射值
public class ReportModel
{
public List<CarListingModel> CarListings {get; set;}
}
public class CarListingModel
{
public long CarId {get; set;}
public long OwnerId {get; set;}
public string CarColor {get; set;}
}
public class GroupedListingModel
{
public long OwnerId {get; set;}
public int TotalCarCount {get; set;}
public string CarColors {get; set;}
}
我已经选择一组数据到CarListingModel对象类似如下:
var list = someData.Where(...).Select(l => new CarListingModel
{
CarId = l.CarId,
OwnerId = l.OwnerId,
CarColor = l.Color
});
我用得到对应于每一个OWNERID的TotalCarCount代码看起来是这样的:
var result = list.GroupBy(p=> p.OwnerId, p=> p.CarId, (key, g) => new GroupedListingModel {
OwnerId = key,
TotalCarCount = g.Count()
}).ToList();
但是,我想要做的是返回一个键值对,其中包含多个属性的对象中的值(例如汽车数量和汽车颜色列表)返回。像这样的东西(这显然是行不通的):
var result = list.GroupBy(p=> p.OwnerId, p=> p.CarId, p=> p.CarColor (key, g1, g2) => new GroupedListingModel
{ OwnerId = key,
TotalCarCount = g1.Count(),
CarColors = string.Join(", ", g2.ToList());
}).ToList();
我曾尝试以不同的方式创建分组上市的模式,这样,但不知道如果我在正确的道路上:
public class GroupedListingModel_2
{
public long OwnerId {get; set;}
public DetailsModel Details {get; set;}
}
public class DetailsModel
{
public int TotalCarCount {get; set;}
public string CarColors {get; set;}
}
现在我想这个数据会更好地结构化为一个字典,但我不确定如何从'列表'获得我想要的结果。