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
Asked
Active
Viewed 163 times
0
-
Since it's an `int`, can you create an `Enum` with the indices you want and [cast the values](https://stackoverflow.com/questions/29482/how-can-i-cast-int-to-enum) when you want to use them? – D M Mar 25 '21 at 14:24
-
If you have a column "grade-name" in db just add to select. If you do not have make private method with switch and put inside select. – azuremycry Mar 25 '21 at 14:27
-
@DM Good idea, but could please suggest how can i do the casting and return it in the result – Muhammad Radwan Mar 25 '21 at 14:27
-
@MuhammadRadwan Can you change the definition of `Tbl_Trohpy_Flag` or is it generated by Sqllite-pcl? – D M Mar 25 '21 at 14:28
-
@azuremycry I don't have such column in the db, can you code the solution please? – Muhammad Radwan Mar 25 '21 at 14:29
-
@DM I can chage it, I created it manually – Muhammad Radwan Mar 25 '21 at 14:30
1 Answers
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