1

In .net core project and postgresql db I am joining 3 tables. I am trying to get enum values in this join. My join looks like

        [HttpGet]
        public IActionResult GetVMs()
        {
            var model =
                from vm in _context.VirtualMachines
                join project in _context.Projects on vm.ProjectId equals project.Id
                join hypervisor in _context.Hypervisors on vm.HypervisorId equals 
                hypervisor.HypervisorId
                join managment in _context.Managements on vm.ManagementId equals managment.Id
                select new
                {
                    Name = vm.Name,
                    IpAddress = vm.IpAddress,
                    DiskSize = vm.DiskSize,
                    Cpu = vm.CPU,
                    Ram = vm.Ram,
                    ImageUrl = vm.ImageUrl,
                    Role = vm.Role,
                    Status = vm.Status,
                    Project = project.Name,
                    Hypervisor = hypervisor.Name,
                    Gateway = managment.Gateway,
                    Netmask = managment.Netmask
                };
            return Ok(model);
        }

I am getting back in postman

{
    "name":"Abstergo",
    "ipAddress":"192.168.0.1",
    "diskSize":25,
    "cpu":16,
    "ram":100,
    "imageUrl":"www.google.com",
    "role":1,
    "status":0,
    "hypervisorId":1,
    "projectId":11,
    "managementId":8
}

How to show role and status enum values?

Arzu Suleymanov
  • 671
  • 2
  • 11
  • 33
  • 1
    Is that what's saved in the database ie enum string value and not the integer value? Or Do you want to convert them in C# bcz that is just .... `(MyEnum) int_val` or `Enum.ToObject(typeof(MyEnum), 1)` – user14492 Nov 25 '19 at 14:54
  • 1
    Do you want the string values of the Enums? Try : Enum.GetName(typeof(Enum Type), "value") or if you just want to parse the value to the Enum : Enum.Parse(typeof(Enum Type), "value") – jdweng Nov 25 '19 at 15:06

2 Answers2

2

The outputs you see for "Role" and "Status" in your Postman response

    "role":1,
    "status":0,

are the enum values, just as the underlying enum type which by default is int.

When you declare an Enum (e.g.):

enum Status
{
    Online,
    Offline
}

the enum values you declare as "Online" or "Offline" will be treated as integer values during runtime. If you want to return the string representation of your enum, you should call the ToString() function on the Enum values.

In your dedicated example, we do not have any information about how the values are stored in the database (sticking to above example: as "0", "1" or as "Online", "Offline"?) and we miss the information on the type they have during runtime: String, int, enum?

So do following: 1. Check how they are stored in database 2. Figure out which type they have during runtime

If they have a enum type during runtime, you can just store the corresponding string value in your response.

If they are not of type enum during runtime, you should cast them first to your corresponding enum types and then return the string values in your response.

rekcul
  • 319
  • 1
  • 7
  • 18
1

If by values you mean their string representations from code then just use .ToString():

[HttpGet]
public IActionResult GetVMs()
{
    var model =
        from vm in _context.VirtualMachines
        join project in _context.Projects on vm.ProjectId equals project.Id
        join hypervisor in _context.Hypervisors on vm.HypervisorId equals 
        hypervisor.HypervisorId
        join managment in _context.Managements on vm.ManagementId equals managment.Id
        select new
        {
            Name = vm.Name,
            IpAddress = vm.IpAddress,
            DiskSize = vm.DiskSize,
            Cpu = vm.CPU,
            Ram = vm.Ram,
            ImageUrl = vm.ImageUrl,
            Role = vm.Role.ToString(),
            Status = vm.Status.ToString(),
            Project = project.Name,
            Hypervisor = hypervisor.Name,
            Gateway = managment.Gateway,
            Netmask = managment.Netmask
        };
    return Ok(model);
}

You could also tinker with the serializer to change enum handling, but that would make it application-wide. In your startup class:

services.AddMvc()
    .AddJsonOptions(options =>
        options.SerializerSettings.Converters.Add(new StringEnumConverter())
    );

See this question for details.

holdn
  • 96
  • 2
  • 6