我在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查询(将类型对象的字段向下转换为更具体的类以进行比较).
https://jira.mongodb.org/browse/CSHARP-900
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。