Hello Everyone my problem is when i add the data to Dictionary in c# and i want to switch (Sortype) string to check how the user wants the order data like this:
string Sortype ="ByDate";
Dictionary<string, Data> MyDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
switch (Sortype)
{
case "ByViews":
WorldInfo.OrderByDescending(AllData => AllData.Value.WorldPDownloads).ToList();
break;
case "ByDownloads":
WorldInfo.OrderBy(AllData => AllData.Value.WorldPDownloads).ToList();
break;
}
In this way, the code Will Not work and the Data does not sorted but when i use this way the code will work but i cant switch (Sortype) because also the code will not work when i switch (SortData) Variable:
Dictionary<string, Data> MyDictionary = JsonConvert.DeserializeObject<Dictionary<string, Data>>(Res.Body.ToString());
var SortData = MyDictionary.OrderByDescending(x => x.Value.ByViews).ToList();
So I'm looking for the right way to do it. Thank you :)
CodePudding user response:
The reason it does not work in your case is because OrderByDescending returns an object after ordering.
You can do something like this
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;
Or
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;
}
CodePudding user response:
I do not know if that is a bug or just missing on purpose but you are setting string Sortype ="ByDate";
without actually having a "ByDate"
in the switch
statement.