簡體   English   中英

Windows Phone - LongListSelector按日期對項目進行分組,並按日期排序

[英]Windows Phone - LongListSelector grouped items by date and ordered by date

我有我的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(); 

此時我們有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 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM