有没有一种简单的方法可以将 ac# 枚举转换为字符串,然后再转换回来?

发布于 2024-10-14 17:42:04 字数 412 浏览 3 评论 0原文

我需要将枚举值列表转换为单个字符串以存储在我的数据库中;然后当我从数据库检索时再次转换回来。

每个枚举的值目前都是一个简单的整数,因此创建一个额外的表来处理这个问题感觉有点过分了。

因此,在下面的示例中,如果用户选择“母亲”、“父亲”和“姐妹”,那么数据库中存储的值将为“0,1,3”

  public enum MyEnum
    {
        Mother = 0,
        Father = 1,
        Gran = 2,
        Sister = 3,
        Brother = 4
    }

,我是 c# 新手,所以不确定是否有一个不错的输出 -开箱即用的方式来做到这一点 - 我在谷歌搜索时找不到任何明显的东西!

提前干杯:) -L

I need to convert a List of enums values to a single string to store in my db; then convert back again when I retrieve from the database.

Each enum's value is currently a simple integer, so it feels a bit overkill to create an extra table to deal with this.

So, in the example below, if the user selects Mother, Father and Sister, then the value stored in the database will be "0,1,3"

  public enum MyEnum
    {
        Mother = 0,
        Father = 1,
        Gran = 2,
        Sister = 3,
        Brother = 4
    }

I'm new to c#, so not sure if there is a nice out-the-box way to do this - I couldn't find anything obvious when google hunting!

Cheers in advance :)
- L

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

拿命拼未来 2024-10-21 17:42:05

如果您将枚举值更改为:

[Flags]
public enum MyEnum
    {
        Mother = 1,
        Father = 2,
        Gran = 4,
        Sister = 8,
        Brother = 16,
    }

那么您可以

将Father和Gran存储为6 Sister,将Brother存储为24等,您不应该通过组合它们来获得重复值

使用二进制数

If you change you enum values to:

[Flags]
public enum MyEnum
    {
        Mother = 1,
        Father = 2,
        Gran = 4,
        Sister = 8,
        Brother = 16,
    }

Then you could store Father and Gran as 6

Sister and Brother as 24 etc

by using binary numbers you should not get duplicate values by combining them

时光暖心i 2024-10-21 17:42:05

以下将根据要求通过“0,1,3”在枚举值数组之间来回转换:

MyEnum[] selection = { MyEnum.Mother, MyEnum.Father, MyEnum.Sister };

string str = string.Join(",", selection.Cast<int>());

MyEnum[] enm = str.Split(',').Select(s => int.Parse(s)).Cast<MyEnum>().ToArray();

The following will convert back and forth between an array of Enum values via "0,1,3" as requested:

MyEnum[] selection = { MyEnum.Mother, MyEnum.Father, MyEnum.Sister };

string str = string.Join(",", selection.Cast<int>());

MyEnum[] enm = str.Split(',').Select(s => int.Parse(s)).Cast<MyEnum>().ToArray();
夜清冷一曲。 2024-10-21 17:42:05

从你的代码来看是

MyEnum a = MyEnum.Mother;
string thestring = a.ToString();
MyEnum b = (MyEnum) Enum.Parse(typeof(MyEnum), thestring);

from your code it is

MyEnum a = MyEnum.Mother;
string thestring = a.ToString();
MyEnum b = (MyEnum) Enum.Parse(typeof(MyEnum), thestring);
静谧幽蓝 2024-10-21 17:42:05

只需使用 ToString转换为名称,并使用 Enum。 TryParse(或Enum.Parse(如果您不在 .NET 4)上转换回来。

如果您希望一个枚举字段包含多个值(例如 MyEnum.Mother | MyEnum.Father),则需要将其转换为 Flags 枚举,如 @WraithNath 建议。否则,您将讨论单独存储每个选项(无法将 MotherFather 存储在当前设置的同一字段中)。

Just use ToString to convert to the name, and the use Enum.TryParse (or Enum.Parse if you're not on .NET 4) to convert back.

If you're wanting one enum field to contain multiple values (e.g MyEnum.Mother | MyEnum.Father), you'll need to convert that to a Flags enum, as @WraithNath suggested. Otherwise you're talking about storing each option separately (there's no way to store Mother and Father in the same field with your current setup).

南风几经秋 2024-10-21 17:42:05

字符串等价

MyEnum value = MyEnum.Father;
value.ToString(); // prints Father

解析

(MyEnum)Enum.Parse(typeof(MyEnum), "Father"); // returns MyEnum.Father

String Equivelant

MyEnum value = MyEnum.Father;
value.ToString(); // prints Father

Parsing

(MyEnum)Enum.Parse(typeof(MyEnum), "Father"); // returns MyEnum.Father
白首有我共你 2024-10-21 17:42:05

Enum 可以与整数值进行转换。这可能是你最好的选择。

如果你真的想使用字符串,ToString将返回“Mother”“Father”“Gran”等。从字符串强制转换只是一个函数:

private MyEnum GetEnumValue(string fromDB)
{
    if( fromDB == "Mother" ) return MyEnum.Mother;
    else if( fromDB == "Father") return MyEnum.Father;
    //etc. etc.
}

编辑:
Ian 对于回溯的回答是更“C#-ey”的方式,并且可扩展性更强(可以更灵活地向枚举添加新值)。

Enums can be cast to and from integer values. That's probably your best bet.

If you really want to use strings, ToString will return "Mother" "Father" "Gran" etc. Casting back from a string would just be a function:

private MyEnum GetEnumValue(string fromDB)
{
    if( fromDB == "Mother" ) return MyEnum.Mother;
    else if( fromDB == "Father") return MyEnum.Father;
    //etc. etc.
}

EDIT:
Ian's answer for casting back is the more "C#-ey" way of doing it, and is far more extensible (handles adding new values to the enum much more flexibly).

牵你的手,一向走下去 2024-10-21 17:42:05

根据 Jamiec 的建议(非常适合您的需求),如果您需要防御性地对您的演员进行编码,请尝试:

if (Enum.IsDefined(typeof(MyEnum), <your database value>))
{
    // Safe to convert your enumeration at this point:
    MyEnum value = (MyEnum)1;   
}

Further to Jamiec's suggestion (which fits well for your need), if you need to defensively code your casting, try:

if (Enum.IsDefined(typeof(MyEnum), <your database value>))
{
    // Safe to convert your enumeration at this point:
    MyEnum value = (MyEnum)1;   
}
宣告ˉ结束 2024-10-21 17:42:04

枚举可以明确地转换为整数

int value = (int)MyEnum.Mother;

   MyEnum value = (MyEnum)1;

从整数转换,对于字符串使用 ToStringEnum.Parse

string value = MyEnum.Mother.ToString();

MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum),"Mother");

Enum's are explicitely able to be cast to/from integers

int value = (int)MyEnum.Mother;

and

   MyEnum value = (MyEnum)1;

For strings use ToString and Enum.Parse

string value = MyEnum.Mother.ToString();

and

MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum),"Mother");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文