0

Query code & results

I'm using Sqlite-pcl to retrieve some data from a database. When the result is displayed, there is a certain field returned as an integer (grade), representing the trophy grade. I want to replace that field with a string value depending on the value, e.g if the value is 0, I want the result to be "Platinum", if the value is 1, the value should be "Silver". Of course there is no such table in the database which holds descriptions for these grades, for this reason I cannot use joins

1 Answers1

0

In the comments you mentioned that you can change the definition of Tbl_Trophy_Flag, so what I would do is create an Enum with the values you want:

public enum TrophyGrade {
    Platinum = 0,
    Silver = 1,
    // ...
}

Change the field type in your model:

public class Tbl_Trophy_Flag {

    // public int grade { get; set; }
    public string grade { get; set; }
}

And cast the value returned from the database:

List<Tbl_Trophy_Flag> res = (from flg in Table<Tbl_Trophy_Flag>()
                             select new Tbl_Trophy_Flag()
                             {
                                 title = flg.title,
                                 description = flg.description,
                                 grade = (TrophyGrade)flg.grade
                             }).ToList();
D M
  • 5,769
  • 4
  • 12
  • 27
  • Niiice, thank you sooooo much, I figured it out by creating a private method to do the trick and it worked, but your method looks more prof, I will give it a try, again thanks for saving my day – Muhammad Radwan Mar 25 '21 at 14:46