question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

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:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
lbnascimentocommented, Jan 28, 2020

@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:

BsonMapper.Global.EnumAsInteger = true;
3reactions
nabeeliocommented, May 19, 2018

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found