WindowsPhone-LongListSelector按日期对项目进行分组并按日期排序
本文关键字:日期 排序 项目 WindowsPhone-LongListSelector | 更新日期: 2023-09-27 18:27:47
我有一个BaseArticle
类,它有PublishedDate
,我想按天对项目进行分组,之后我想按日期和时间订购。目前,我可以按天对它们进行分组,这很有效(我创建了以字符串形式返回日期的get属性PublishedDateString
):
public List<Group<BaseArticle>> GetArticlesGroups()
{
return GetItemGroups<BaseArticle>(Articles, a => a.PublishedDateString);
}
private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, string> getKeyFunc)
{
IEnumerable<Group<T>> groupList = from item in itemList
group item by getKeyFunc(item) into g
orderby g.Key
select new Group<T>(g.Key, g);
return groupList.ToList();
}
我是LINQ的新手,我不知道如何编辑我的GetItemGroups
方法,以便在项目分组后按日期排序。感谢
您只是缺少了另一个orderby
语句。
我没有完整的BaseArticle
类,所以我只想通过创建一个Song/Artist类来简化这一点,比如:
public class sample_model
{
public sample_model(string artist, string song, string extra = "")
{
this.Artist = artist;
this.Song = song;
this.Extra = extra;
}
public string Artist { get; set; }
public string Song { get; set; }
public string Extra { get; set; }
}
然后,我们将通过进行来列出sample_model
private ObservableCollection<sample_model> CreateData()
{
ObservableCollection<sample_model> my_list = new ObservableCollection<sample_model>();
my_list.Add(new sample_model("Faith + 1", "Body of Christ", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Christ Again", "A Track"));
my_list.Add(new sample_model("Faith + 1", "A Night With the Lord", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Touch Me Jesus", "A Track"));
my_list.Add(new sample_model("Faith + 1", "I Found Jesus (With Someone Else)", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Savior Self", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Christ What a Day", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Three Times My Savior", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Jesus Touched Me", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Lord is my Savior", "A Track"));
my_list.Add(new sample_model("Faith + 1", "I Wasn't Born Again Yesterday", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Pleasing Jesus", "A Track"));
my_list.Add(new sample_model("Faith + 1", "Jesus (Looks Kinda Hot)", "A Track"));
my_list.Add(new sample_model("Butters", "What What", "B Track"));
// add a duplicate song for our example
my_list.Add(new sample_model("Faith + 1", "Body of Christ", "A Track"));
return my_list;
}
// create our list
ObservableCollection<sample_model> DataSource = CreateData();
在这一点上,我们有两个艺术家(信仰+1和黄油)。
我们可以通过这个按艺术家名称查询和分组这个数据列表
var query = from item in DataSource
group item by item.Artist into ArtistGroup
orderby ArtistGroup.Key
select ArtistGroup;
foreach (var ArtistGroup in query)
{
// this will loop twice. Once for Butters and Once for Faith + 1
// it will order by Key which is the Artist Name
// so Butters will be first then Faith + 1
// but the Songs will not be ABC order, it will be order the same way we inserted them into the list
}
现在回到你最初的问题!您希望列表按艺术家排序,并对每个艺术家的歌曲进行排序。那么你必须有另一个orderby statement
// before we group by item.Artist, we sort by item.Song using another `orderby`
var query = from item in svm.DataSource
orderby item.Song
group item by item.Artist into ArtistGroup
orderby ArtistGroup.Key
select ArtistGroup;
foreach (var ArtistGroup in query)
{
// this will loop twice. Once for Butters and Once for Faith + 1
// it will order by Key which is the Artist Name
// so Butters will be first then Faith + 1
// this time the sub list of each Artist will have their Songs sorted as well
// For example, under Faith + 1 you will see
// "A Night With the Lord" follow by
// "Body of Christ" (TWICE) remember we put 2 in
}