1

I have a .NetCore 3 Entity Framework controller that returns an object like this:

var games = await _context.Game
   .Select(x => new GameEntity
   {
       Id = x.Id,
       Title = x.Title,
       GameCharacterClasses = x.GameCharacterClasses

    }).ToListAsync();   

The GameEntity class looks like this:

public partial class GameEntity
{
    public GameEntity()
    {
        GameCharacterClasses = new HashSet<GameCharacterClasses>();
    }

    public long Id { get; set; }
    public string Title { get; set; }

    public virtual ICollection<GameCharacterClasses> GameCharacterClasses { get; set; }
}

GameCharacterClasses is an array with the Id of the game and the Id of the characterClass.

The thing is, I really need the characterClass name and not the Id of the game or characterClass.

Is there a way to make a code-based table or variable so I can look-up the needed characterClass name based on the Id?

Like, if the GameCharacterClasses array contains the Ids(GameId, CharacterClassId)

[(5, 1), (5, 4), (5, 7)] 

Then the array should contain

'Wizard', 'Fighter', 'Cleric'?

I tried using a complicated if/then tree, but it got out of hand.

I'd like to have some type of look-up based variable if that's possible...like:

0 : Thief
1 : Wizard
2 : Paladin
3 : Assassin
 etc...

Is there a way to do that in c#?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • 1
    An `enum`, a static `Dictionary`... It depends on your use case – Emanuel Vintilă Apr 14 '20 at 14:30
  • I like to use [this approach](https://stackoverflow.com/questions/3422407/finding-an-enum-value-by-its-description-attribute) for things like that – mu88 Apr 14 '20 at 14:43
  • Why not introduce a `CharacterClass` class + table? – Gert Arnold Apr 14 '20 at 19:09
  • @GertArnold what do you mean? I'm sorry I'm confused :) – SkyeBoniwell Apr 14 '20 at 19:25
  • So `CharacterClassId` is a foreign key to `CharacterClass` records that consist of Id + Name. You'll have a many-to-many relationship between `Game` and `CharacterClass`. – Gert Arnold Apr 15 '20 at 06:45
  • Does this help: https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions? If you use a converter you could have an enum that automagically gets converted to an int when persisted to the db and gets turned back into an enum when loaded. – Adam Benson Apr 15 '20 at 09:17

1 Answers1

1

You have an enum named CharacterClass

public enum CharacterClass
{
    Thief= 0,
    Wizard= 1,
    Paladin= 2,
    Assassin= 3,
}

Then you will call it like this using Enum.GetName

public string GetNameFromId(int charClassId)
{
    return Enum.GetName(typeof(CharacterClass), charClassId);
}
Markuzy
  • 483
  • 6
  • 10
  • Thanks so if I had an array with 4 Ids of CharacterClasses, I could loop through it with 'GetNameFromId' function? – SkyeBoniwell Apr 14 '20 at 17:26
  • 1
    Yes, assuming that you are not parsing a huge (thousands ~ millions) of ids, it should be ok this way looping it, and if using it when required. Otherwise, consider a dictionary like Emanuel Vintilă suggested. – Markuzy Apr 14 '20 at 17:48