简体   繁体   中英

How to display the string Enum values instead of the number value using SQL

I'm trying to display a list of all Deliveries with the status Dispatched . However, its only returning the number value of the status as opposed to the actual string value . I think this is because I have used Enum to store my status values ?

I wish to display the word Dispatched instead of the number value that it represents in the Enum .

I'm developing in ASP.Net MVC and I'm using the query builder in VS2013.

I'm not sure how to approach this, can anyone please suggest an easy to understand solution using SQL.

Let me know if any additional code is required, and thank you in advance!

Here's the Query I want but it doesn't work:

SELECT Delivery.[Status], 
COUNT(Delivery.DeliveryID) AS Dispatched_Status 
FROM Delivery 
WHERE Delivery.[Status] = 'Dispatched'
GROUP BY Delivery.[Status];

Here's the Query that does work but returns a number value. I tried it this way because Enum stores the string value as a number:

SELECT Delivery.[Status], 
COUNT(Delivery.DeliveryID) AS Dispatched_Status 
FROM Delivery 
WHERE Delivery.[Status] = '1'
GROUP BY Delivery.[Status];

PS I'm aware that status is a reserved word - will be following the correct naming conventions in future.

Delivery Table Definion

在此输入图像描述

It sounds like you just need to add a lookup table in you DB. Something like

CREATE TABLE [dbo].[StatusLookup](
    [StatusID] [int] NOT NULL,
    [StatusName] [varchar](64) NOT NULL,
    [StatusDescription] [varchar](max),
)

INSERT INTO [dbo].[StatusLookup]([StatusID],[StatusName],[StatusDescription]
VALUES(1, 'Dispatched', 'A dispatched record')
...

Note you'll have to manually do this and make sure to populate it with values that match up with your enum.

Then your query would be

SELECT StatusLookup.[StatusName], 
COUNT(Delivery.DeliveryID) AS Dispatched_Status 
FROM Delivery 
JOIN StatusLookup ON Delivery.Status = StatusLookup.StatusID
WHERE StatusLookup.[StatusName] = 'Dispatched'
GROUP BY StatusLookup.[StatusName];

Enums are stored as integers by default.

You can add a separate varchar or nvarchar field to your database table to hold the description of the enum, and populate it using something like the below:

string selectedEnumDescription = Enum.GetName(typeof(DeliveryStatusEnum), Delivery.Status)

The exact implementation depends on how you are saving your records, and what the actual properties and enum names are.

You can then just select the description column in your SQL query.

Either that or you could store the actual enum values and descriptions within a separate table and do a join.

You can store enum in database as a number, usually a small number - the exact type depends on your database. When you read it - you convert a number to enum and work in your code with the enum . When you need to display it, you can call a ToString() method on that enum, for example

public enum Foo
{
    A,
    B
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Foo.A.ToString()); // Prints    A
    }
}

See it working

You can also use description attribute and print that, see examples here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM