Enum as int
See original GitHub issueHi,
I’m trying to do a RegisterType
for an enum type to convert it into an integer before saving it… mainly to save on space. Holding 50k+ rows with a lengthy string value for an enum seems like a bit of a waste to me. It appears to be getting ignored, though. Any ideas? Maybe I’m missing something really simple but I’m not sure what.
Thanks!
enum SomeType
{
SOME_SUPER_LONG_ENUM_NAME,
MAYBE_A_SHORTER_NAME,
}
BsonMapper.Global.RegisterType<SomeType>(
serialize: (s) => ((int)s).ToString(),
deserialize: (bson) => {
return (SomeType)Enum.Parse(typeof(SomeType), bson);
}
);
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Get int value from enum in C# - Stack Overflow
If you want to convert to int try (in case of an enum Sides : int) [...] object val = Convert.ChangeType(side, typeof(int)); [...]....
Read more >How to cast an enum to int in C#
Overview. To convert an enum to int , we can: Typecast the enum value to int . Use the ToInt32 method of the...
Read more >C# Enumerations Type - Enum
In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant...
Read more >How to Use Enum in C# | LoginRadius Blog
To get the value of enum we can simply typecast it to its type. In the first example, the default type is int...
Read more >How to Get int value from Enum in C# with examples
Solution 1: Use Type cast to get int value from enum · Solution 2: Use Convert.ToInt32() method to get the integer value from...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@darcome This feature is avaliable in LiteDB v5. If you’re using the global mapper, you can configure it to treat enums as ints with the following command:
Thanks for this! It’s a great starting point. Might be good to note on the wiki too, might be something others might be interested in doing as well.
Much appreciated!