我有简单的类型Question
:
public class Question
{
public Question(string id)
{
Id = id;
Tags = new List<string>();
}
public string Id { get; private set; }
public IList<string> Tags { get; set; }
}
我已经定义了此类问题的样本集合:
var q1 = new Question("q1") { Tags = new List<string>() {"aa", "bb"} };
var q2 = new Question("q2") { Tags = new List<string>() {"aa"} };
var q3 = new Question("q3") { Tags = new List<string>() {"aa", "bb", "cc"} };
var q4 = new Question("q4");
var questions = new List<Question>() {q1, q2, q3, q4};
现在我需要从给定的子集中找到所有问题,其中至少包含所有标签。子集定义如下:
string[] tags = new[] {"aa", "bb"};
我用来获得所需结果的查询是:
var result = questions.Where(x => !tags.Except(x.Tags).Any()).ToList();
结果是 2 个问题的列表:q1和q3,在我使用 linq-to-objects 时可以正常工作。
不幸的是,当我试图查询这些问题,这些问题现在保留在 RavenDB 中时,我得到了一个例外:
var result = DocumentSession.Query<Question>()
.Where(x => !tags.Except(x.Tags).Any()).ToList();
结果是:
System.InvalidOperationException: Cannot understand how to translate value(Core.Commands.GetQuestions+<>c__DisplayClass0).tags.Except(x.Tags)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.GetPath(Expression expression, String& path, Type& memberType, Boolean& isNestedPath)
at Raven.Client.Linq.DynamicQueryProviderProcessor`1.GetMember(Expression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitAny(MethodCallExpression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitEnumerableMethodCall(MethodCallExpression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitMethodCall(MethodCallExpression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitExpression(Expression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitExpression(Expression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitQueryableMethodCall(MethodCallExpression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitMethodCall(MethodCallExpression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.VisitExpression(Expression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.GetLuceneQueryFor(Expression expression)
at Raven.Client.Linq.RavenQueryProviderProcessor`1.Execute(Expression expression)
at Raven.Client.Linq.DynamicRavenQueryProvider`1.Execute(Expression expression)
at Raven.Client.Linq.DynamicRavenQueryProvider`1.System.Linq.IQueryProvider.Execute(Expression expression)
at Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
...
如何使用 RavenDB 执行我想要的操作?