具有以下模型(爲了簡化,我發佈了這些接口)。在一個新的列表類型中合併3個不同的類型列表
public class LengthViewModel
{
public int Length { get; set; }
public string Category { get; set; }
}
public class SlopeViewModel
{
public int Slope { get; set; }
public string Category { get; set; }
}
public class RatingViewModel
{
public double Rating { get; set; }
public string Category { get; set; }
}
然後我在另一個ViewModel中有一個ObservableCollection的每個類型。
public ObservableCollection<LengthViewModel> Lengths { get; set; }
public ObservableCollection<SlopeViewModel> Slopes { get; set; }
public ObservableCollection<RatingViewModel> Ratings { get; set; }
我需要將上述列表轉換爲一個列表,下面是它應該創建的新列表類型。
public ObservableCollection<LengthSlopeRatingViewModel> Aggregate { get; set; }
public class LengthSlopeRatingViewModel
{
public string Category { get; set; }
public int Length { get; set; }
public int Slope { get; set; }
public double Rating { get; set;}
}
我到目前爲止的嘗試,但似乎堅持如何選擇每個轉換列表的屬性。
var lengths = Lengths.Select(p => new LengthSlopeRatingViewModel
{
Category = p.Category,
Length = p.Length
});
var slopes = Slopes.Select(p => new LengthSlopeRatingViewModel
{
Category = p.Category,
Slope = p.Slope
});
var ratings = Ratings.Select(p => new LengthSlopeRatingViewModel
{
Category = p.Category,
Rating = p.Rating
});
// Concat and group them, then select new type again with the properties?
CourseRatings = lengths.Concat(slopes)
.Concat(ratings)
.GroupBy(p => p.Category)
.Select(g => g.ToList())
.As<IEnumerable<LengthSlopeRatingViewModel>>()
.ToObservableCollection();
例如,如果您有長度,坡度和評級,Category = "Black"
與Category = "Blue"
另一個實例的實例,我應該得到LengthSlopeRatingViewModel的兩個實例,一個與Category = "Black"
和第一場比賽的相應值和一個與Category = "Blue"
。
不應該'選擇'後'GroupBy'是'SelectMany'? – KMoussa
在提出問題時,請花更多的努力來設置代碼的格式 - 不需要將其大部分縮小到正確的位置。我這次編輯了這個問題。這聽起來像你真正想要的是幾個連接,說實話......你應該也說出你想要發生什麼,如果這個類別只在一個或兩個原始集合中。 –
我承認它需要更多的工作來解決這個問題,現在我看到了這個連接,我被卡住了一點,感謝編輯:-) –