Azure Mobile中的一个表有一个定义为Number的列。在客户端,该列映射到枚举。我有一个JSON转换器,它将枚举值封送到int(下面的代码)。
class CompletedStateConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(CompletedState);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return CompletedState.NotCompleted;
int intVal = serializer.Deserialize<int>(reader);
CompletedState gameCompleted = (CompletedState)intVal;
return gameCompleted;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
CompletedState gameCompleted = (CompletedState)value;
int number = (int)gameCompleted;
serializer.Serialize(writer, number);
}
}
一切正常,直到我需要对用于从该表读取数据的其中一个查询进行更改。
data = await App.GameTable
.Take(15)
.OrderByDescending(g => g.LastMoveTime)
.Where(g => g.Completed == CompletedState.NotCompleted)
.ToListAsync();
注意where子句选择Completed状态为NotCompleted的项(枚举列表中的值为0)。
在后端,查询被转换为字符串,而不是使用JSON转换器应提供的数字。
这里是query.getComponents()
的内容{
"filters":
{
"operator":"And",
"left":{"queryString":"(Completed eq 'NotCompleted')","args":[],"type":"LiteralExpression"},
"right":
{
"operator":"Or",
…removed
"type":"BinaryExpression"
},
"type":"BinaryExpression"
},
"selections":[],
"projection":null,
"ordering":{"LastMoveTime":false},
"skip":null,
"take":15,
"table":"Game",
"includeTotalCount":false,
"version":4
}
我预计查询会利用JSON转换器将枚举转换为数字。为什么不这样做,我该怎么做才能使它发挥作用?
谢谢