索引枚举的值,而不是字符串 [英] Index the value of an Enum, not the string

查看:24
本文介绍了索引枚举的值,而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Solrnet 索引 Enum 的值而不是其字符串表示?

Is it possible to index the value of an Enum instead of its string representation using Solrnet?

说我必须遵循枚举:

[Serializable]
[Flags] 
public enum Gender
{
  Male = 0,
  Female = 1
}

并向 User 类的 Gender 属性添加 solr 属性:

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)

然后它会将男性"索引为性别而不是 0.是否可以将其索引为 0?

then it will index 'Male' as gender instead of 0. Is it possible to have it index 0 instead?

我不想像 Ondrej 建议的那样为此添加额外的属性.

I'd prefer not to add an extra property for this, like Ondrej proposes.

推荐答案

您可以通过实施 ISolrFieldSerializer.如果类型为 IsEnum,则通过强制转换为 int 进行序列化.否则委托给 DefaultFieldSerializer使用其他 字段序列化器作为参考.

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.

连接你的字段序列化器取决于选择的 IoC 容器,检查容器的文档.

Hooking up your field serializer depends on the chosen IoC container, check the container's documentation.

这篇关于索引枚举的值,而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆