MongoDB无法确定表达式错误的序列化信息 [英] MongoDB Unable to determine the serialization information for the expression error

查看:314
本文介绍了MongoDB无法确定表达式错误的序列化信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据具有以下结构

public enum ParamType
{
    Integer=1,
    String=2,
    Boolean=3,
    Double=4
}

public class Gateway
{
    public int _id { get; set; }
    public string SerialNumber { get; set; }
    public List<Device> Devices { get; set; }
}

public class Device
{        
    public string DeviceName { get; set; }
    public List<Parameter> Parameters { get; set; }
}

public class Parameter
{
    public string ParamName { get; set; }
    public ParamType ParamType { get; set; }
    public string Value { get; set; }
}

我在MongoDB数据库中填充了Gateway的10个文档对象. 现在,我要查询所有包含具有ParamName作为目标温度"的参数且其Value> 15的设备的网关.

I filled 10 document objects of Gateway in a MongoDB database. Now I want to query all those gateways which contains a device having Parameter with ParamName as "Target Temperature" and whose Value > 15.

我创建了以下查询

var parameterQuery = Query.And(Query<Parameter>.EQ(p => p.ParamName, "Target Temperature"), Query<Parameter>.GT(p => int.Parse(p.Value), 15));

var deviceQuery = Query<Device>.ElemMatch(d => d.Parameters, builder => parameterQuery);

var finalQuery = Query<Gateway>.ElemMatch(g => g.Devices, builder => deviceQuery);

但是当我运行它时,它给出了异常

But when I run this, it is giving an exception

Unable to determine the serialization information for the expression: (Parameter p) => Int32.Parse(p.Value)

请指出我错了.

推荐答案

错误提示,您不能在查询中使用Int32.Parse.该lambda表达式用于获取属性的名称,并且无法理解Int32.Parse是什么.

As the error suggests, you can't use Int32.Parse inside your query. This lambda expression is used to get out the name of the property and it doesn't understand what Int32.Parse is.

如果要查询字符串,则需要使用字符串值进行比较:

If you are querying a string, you need to use a string value for comparison:

var parameterQuery = Query.And(Query<Parameter>.EQ(p => p.ParamName, "Target Temperature"), Query<Parameter>.GT(p => p.Value, "15"));

但是,由于您正在使用GT,所以这可能不是您想要的.要在此比较中将其视为数字,您需要将该值实际上是mongo中的int,因此您需要更改属性的类型:

However, that's probably not what you want to do since you're using GT. To be treated as a number for this comparison you need the value to actually be an int in mongo, so you would need to change the type of your property:

public class Parameter
{
    public string ParamName { get; set; }
    public ParamType ParamType { get; set; }
    public int Value { get; set; }
}

var parameterQuery = Query.And(Query<Parameter>.EQ(p => p.ParamName, "Target Temperature"), Query<Parameter>.GT(p => p.Value, 15));

这篇关于MongoDB无法确定表达式错误的序列化信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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