我有一个List
平的ViewModels
。
var initialList = new List<ViewModel>
{
new ViewModel { OuterCaption = "A outer", InnerCaption = "A inner", Caption = "E", Id = 8},
new ViewModel { OuterCaption = "B outer", InnerCaption = "B inner", Caption = "A", Id = 5},
new ViewModel { OuterCaption = "A outer", InnerCaption = "B inner", Caption = "A", Id = 12},
new ViewModel { OuterCaption = "B outer", InnerCaption = "B inner", Caption = "B", Id = 9},
new ViewModel { OuterCaption = "A outer", InnerCaption = "B inner", Caption = "B", Id = 23},
new ViewModel { OuterCaption = "B outer", InnerCaption = "C inner", Caption = "A", Id = 10},
new ViewModel { OuterCaption = "A outer", InnerCaption = "B inner", Caption = "C", Id = 16},
new ViewModel { OuterCaption = "A outer", InnerCaption = "A inner", Caption = "C", Id = 4},
new ViewModel { OuterCaption = "A outer", InnerCaption = "A inner", Caption = "D", Id = 7},
new ViewModel { OuterCaption = "C outer", InnerCaption = "A inner", Caption = "A", Id = 31},
new ViewModel { OuterCaption = "B outer", InnerCaption = "A inner", Caption = "A", Id = 19},
new ViewModel { OuterCaption = "B outer", InnerCaption = "A inner", Caption = "B", Id = 20},
new ViewModel { OuterCaption = "A outer", InnerCaption = "A inner", Caption = "A", Id = 1},
new ViewModel { OuterCaption = "A outer", InnerCaption = "A inner", Caption = "B", Id = 3}
}.OrderBy(vm => vm.OuterCaption).ThenBy(vm => vm.InnerCaption).ThenBy(vm => vm.Caption);
其中每个ViewModel
如下。
class ViewModel
{
public string OuterCaption { get; set; }
public string InnerCaption { get; set; }
public string Caption { get; set; }
public int Id { get; set; }
}
现在我想按 分组List
,OuterCaption
然后按InnerCaption
。我可以这样分组List
。OuterCaption
var list = initialList.GroupBy(vm => vm.OuterCaption).ToList();
并在我View
这样使用它并GroupBy
在我的View
.
foreach (var outerGrouping in list)
{
Console.WriteLine(outerGrouping.Key);
Console.WriteLine("~~~~~~~~~~~~~~~~~~");
foreach (var innerGrouping in outerGrouping.GroupBy(vm => vm.InnerCaption))
{
Console.WriteLine(innerGrouping.Key);
foreach (var vm in innerGrouping)
{
Console.WriteLine("Caption: " + vm.Caption + " Id:" + vm.Id);
}
Console.WriteLine("------------------");
}
Console.WriteLine("~~~~~~~~~~~~~~~~~~");
}
但我想在 a 中创建一个IGrouping
而IGrouping
不是在我GroupBy
的. 我确实认为我应该为我的. 我如何使用 my 实现这一点,以便我可以从我的中删除?Linq
View
IReadOnlyList<IGrouping<string, IGrouping<string, ViewModel>>>
View
initialList
Linq
View