Windows Phone - LongListSelector按日期对项目进行分组,并按日期排序

时间:2014-10-24 14:57:17

标签: c# linq windows-phone-8 windows-phone longlistselector

我的BaseArticle班级有PublishedDate,我希望按天分组,之后我想按日期和时间订购。现在我可以在白天对它们进行分组并且它可以工作(我创建了get property 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方法,以便在分组后按日期订购商品。感谢

1 个答案:

答案 0 :(得分:3)

您只是错过了另一个orderby声明。

我没有完整的BaseArticle课程,所以我通过创建一个类似的歌曲/艺术​​家课程来实现这一目的:

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(); 

此时我们有2位艺术家(Faith + 1和Butters)。

我们可以通过执行此操作

按艺术家姓名查询和分组此数据列表
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 
}