转换.NET MongoDB C#Driver不支持的对象

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

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

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

“EntityAttribute”上的“Value”对象显然可以是任何类型的对象(string,int,float等),这就是意图.

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

var results = entities.AsQueryable<Entity>()
                .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<Entity>()
                        .Where(x => x.Type == "dogs")
                        .Intersect()
                        .Where(x => x.Attributes.Any(a => a.Key == "age" && (int)a.Value > 5)).ToList();

有没有办法通过LINQ,使用MongoDB CSharp LINQ驱动程序实现这一点?我可以使用Query.EQ,Query.GT和Query.LT获得所有必需的查询,但如果可能的话我宁愿使用LINQ.

解决方法:

你是对的,C#驱动程序目前不支持这种形式的LINQ查询(将类型对象的字段向下转换为更具体的类以进行比较).

我创建了一个请求此功能的JIRA票证.看到:

https://jira.mongodb.org/browse/CSHARP-900

相关文章

MongoTemplate 是Spring Data MongoDB 中的一个核心类,为 S...
笔者今天要分享的是一个项目重构过程中如何将数据库选型由原...
mongodb/mongoTemplate.upsert批量插入更新数据的实现
进入官网下载官网安装点击next勾选同意,点击next点击custom...
头歌 MongoDB实验——数据库基本操作
期末考试复习总结