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;}
}
現在我想這個數據會更好地結構化爲一個字典,但我不確定如何從'列表'獲得我想要的結果。