Is it possible to index the value of an Enum instead of its string representation using Solrnet?
Say I have to following enum:
[Serializable]
[Flags]
public enum Gender
{
Male = 0,
Female = 1
}
and add a solr attribute to the Gender
property of a User
class:
[SolrField("gender")]
public virtual Gender Gender { get; set; }
If I index the entity using:
solr.Add(user)
then it will index 'Male' as gender instead of 0. Is it possible to have it index 0 instead?
Edit: I'd prefer not to add an extra property for this, like Ondrej proposes.
You can do this by implementing ISolrFieldSerializer. If the type IsEnum, serialize by casting to int. Otherwise delegate to DefaultFieldSerializer. Use the other field serializers for reference.
Hooking up your field serializer depends on the chosen IoC container, check the container's documentation.
Try this:
[SolrField("gender")]
public int GenderAsInt
{
get { return (int) Gender; }
set { Gender = (Gender) value; }
}
public virtual Gender Gender { get; set; }
Also note that declaring your enum as [Flags]
doesn't make much sense:
- There will hardly be anyone both
Male
andFemale
Male
will be interpreted as the default in respect to the current values of enum fields
来源:https://stackoverflow.com/questions/8668275/index-the-value-of-an-enum-not-the-string