在 ListView Xamarin 中对列表的列进行分组

Group Columns of a List in a ListView Xamarin

我是 Xamarin 和 C# 的新手,我希望得到你的帮助:我有一个这样组织的列表:

ID      School   Student    RegistrationDate
1         A       Ana      21/01/2021 13:00:02
2         A       John     21/01/2021 10:00:02
3         B       Anthony  13/05/2021 10:00:02
4         B       Ana      10/02/2020 10:00:02
5         B       Albert   21/01/2022 10:00:02
6         C       Ana      21/01/2021 10:00:02
7         C       Sheila   22/01/2021 10:00:02
8         C       Ana      26/12/2021 10:00:02

我打算在 ListView 中只显示每所学校的一条记录。像这样:

School  RegistrationDate
A        21/01/2021
B        13/05/2021
C        26/12/2021

我正在使用 SQLite,它 returns 是一个数据列表。 这是我的 MVVM:

 public ObservableCollection<StudentSchool> sc = new ObservableCollection<StudentSchool>();
    public ObservableCollection<StudentSchool> SC
    {
        get => sc;
        set => sc= value;
    }
List<StudentSchool> tmp = await  App.Database.GetSchoolStudent();
   public GetData{
 foreach (var item in tmp)
  {
      var newList = tmp
      .GroupBy(x => new { x.School });  
       //How to fill my SC list?                                                                                                   
    }
 }   
                

这是我的观点:

<ListView ItemsSource="{Binding MyList}">
            <ListView.Header>
                <Grid RowDefinitions="*"
                          ColumnDefinitions="*,*,*">
        <Label Grid.Row="0" Grid.Column="0"Text="Student"/>
         <Label Grid.Row="0" Grid.Column="1" Text="RegistrationDate" />        
                </Grid>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid RowDefinitions="Auto"
                                  ColumnDefinitions="*,*,*">
                            <Label Grid.Row="0" Grid.Column="0" Text="{Binding Student}" />
                            <Label Grid.Row="0" Grid.Column="1" Text="{Binding RegistrationDate, StringFormat='{}{0:dd/MM/yyyy}'}" />
                            
                            </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

如何填写我的 SC 列表?

你可以使用LINQ:

来实现
var result = tmp.GroupBy(x => x.School)
.Select(y => new { y.First().School, y.First().RegistrationDate })
.OrderBy(z => z.RegistrationDate);

然后:

foreach( var item in result)
{
     Console.WriteLine(item);
}