简体   繁体   English

如何使用SQL显示字符串枚举值而不是数值

[英]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 . 我正在尝试显示status Dispatched的所有Deliveries列表。 However, its only returning the number value of the status as opposed to the actual string value . 但是,它只返回status number value而不是实际的string value I think this is because I have used Enum to store my status values ? 我想这是因为我使用Enum来存储我的status values

I wish to display the word Dispatched instead of the number value that it represents in the Enum . 我希望显示单词Dispatched而不是它在Enum表示的number value

I'm developing in ASP.Net MVC and I'm using the query builder in VS2013. 我正在开发ASP.Net MVC,我正在使用VS2013中的query builder

I'm not sure how to approach this, can anyone please suggest an easy to understand solution using SQL. 我不知道如何处理这个问题,任何人都可以使用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: 我这样试了,因为Enum将字符串值存储为数字:

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. PS我知道status是一个保留字 - 将来会遵循正确的命名约定。

Delivery Table Definion 交货表定义

在此输入图像描述

It sounds like you just need to add a lookup table in you DB. 听起来你只需要在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: 您可以向数据库表中添加单独的varcharnvarchar字段以保存枚举的描述,并使用以下内容填充它:

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. 然后,您只需在SQL查询中选择description列即可。

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 . 当你阅读它 - 你将一个数字转换为enum并使用enum在你的代码中工作。 When you need to display it, you can call a ToString() method on that enum, for example 当您需要显示它时,您可以在该枚举上调用ToString()方法,例如

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 您还可以使用description属性并打印出来,请参阅此处此处的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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