投射.NET MongoDB C#驱动程序不支持的对象

 手机用户2602916275 发布于 2023-01-31 20:44

我将文档存储在MongoDB中,由以下类表示:

public class Entity
{
    public string Id;
    public string Name;
    public List Attributes = new List();
}

public class EntityAttribute
{
    public string Key;
    public object Value;
    public string DataType;
}



很明显,“ EntityAttribute”上的“ Value”对象可以是任何类型的对象(字符串,整数,浮点型等)。

当我的应用尝试使用关系运算符(例如“ <”或“>”)执行查询时,它将把“ EntityAttribute.Value”对象强制转换为适当的类型(以便使用关系运算符)。以下是一个示例:

var results = entities.AsQueryable()
                .Where(x => x.Type == "dogs" && 
                    x.Attributes.Any(a => a.Key == "age" && (int)a.Value > 5)).ToList();


这将产生以下异常:

Unsupported where clause: ((Int32)a.Value > 5).


我正在使用MongoDB CSharp Linq驱动程序(http://docs.mongodb.org/ecosystem/tutorial/use-linq-queries-with-csharp-driver/)。我的假设是不支持强制转换。是这样吗

作为比较,这种类型的语法(linq查询)在RavenDB的LINQ驱动程序中确实起作用,即:

var results = session.Query()
                        .Where(x => x.Type == "dogs")
                        .Intersect()
                        .Where(x => x.Attributes.Any(a => a.Key == "age" && (int)a.Value > 5)).ToList();


是否有任何方法可以使用MongoDB CSharp LINQ驱动程序通过LINQ来完成此任务?

我可以使用Query.EQQuery.GTQuery.LT来获得所有必需的查询,但是如果可能的,我宁愿使用LINQ。

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有