簡體   English   中英

如何在向字典添加數據后對其進行排序 (C#)

[英]How To sort Dictionary After Adding data to it (C#)

大家好,我的問題是當我將數據添加到 c# 中的字典時,我想切換 (Sortype) 字符串以檢查用戶如何需要這樣的訂單數據:

                         string SortType="ByDownloads";
                         Dictionary<string, Data> WorldInfo = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
                            switch (SortType)
                            {
                                case "ByDownloads":
                                    WorldInfo.OrderByDescending(AllData => AllData.Value.PostDownloads).ToList();
                                    break;
                                case "ByViews":
                                    WorldInfo.OrderBy(AllData => AllData.Value.PostViews).ToList();
                                    break;
                            }

這樣,代碼將不起作用並且數據不會排序,但是當我使用這種方式時,代碼將起作用:

Dictionary<string, Data> WorldInfo = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
var NewSortedDictionary = WorldInfo.OrderByDescending(AllData => AllData.Value.PostViews).ToList();

所以我正在尋找正確的方法來做到這一點並使用 (WorldInfo) 字典和開關 (SortType) 然后我使用它而不是 (NewSortedDictionary)。 謝謝:)

感謝所有對此問題發表評論的人:)我用這種方式修復了它,我希望它能幫助將來的人:

            SortType="ByDownloads";
            Dictionary<string, Data> NormallDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
            List<KeyValuePair<string,Data>> MySortedDictionary=  new List<KeyValuePair<string, Data>>();
            switch (SortType)
            {
                case "ByDownloads":
                MySortedDictionary = NormallDictionary.OrderByDescending(x => x.Value.Downloads).ToList();
                break;
                case "ByViews":
                MySortedDictionary = NormallDictionary.OrderByDescending(x => x.Value.Views).ToList();
                break;
            }

現在您可以隨心所欲地使用 MySortedDictionary

它在您的情況下不起作用的原因是因為 OrderByDescending 在訂購后返回 object。

你可以這樣做

        Dictionary<string, Data> MyDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString()));
        var data = Sortype == "ByViews" ? WorldInfo.OrderByDescending(AllData => AllData.Value.WorldPDownloads).ToList() : Sortype == 'ByDownloads' ? WorldInfo.OrderBy(AllData => AllData.Value.WorldPDownloads).ToList() : null;

或者

        string Sortype = "ByDate";
        Dictionary<string, Data> MyDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
        switch (Sortype)
        {
            case "ByViews":
                WorldInfo = WorldInfo.OrderByDescending(AllData => AllData.Value.WorldPDownloads).ToList();
                break;
            case "ByDownloads":
                WorldInfo =WorldInfo.OrderBy(AllData => AllData.Value.WorldPDownloads).ToList();
                break;
        }

我不知道這是一個錯誤還是故意遺漏,但您正在設置string Sortype ="ByDate"; switch語句中實際上沒有"ByDate"

暫無
暫無

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

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