查找对象至少包含从子使用RavenDB和LINQ的所有元素 [英] Finding objects which contains at least all elements from subset using RavenDB and LINQ

查看:145
本文介绍了查找对象至少包含从子使用RavenDB和LINQ的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单类型问题

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; }            
}

这样的问题

我已经定义了样品采集:

I have defined sample collection of such questions:

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};

现在,我需要找到所有的问题,它至少包含所有标签,从给定的子集。子集的定义如下:

Now I need to find all questions, which contains at least all tags, from given subset. Subset is defined below:

string[] tags = new[] {"aa", "bb"};

我用它来获得所需结果的查询是:

The query which I use to get desired result is:

var result = questions.Where(x => !tags.Except(x.Tags).Any()).ToList();

结果是2个问题的清单: Q1 Q3 ,它正常工作,而我使用LINQ到对象。

Result is a list of 2 questions: q1 and q3, which works properly while I'm using linq-to-objects.

不幸的是,当我试图询问这些问题,而现在坚持RavenDB,我得到一个异常:

Unfortunately, while I'm trying to query such questions, which are now persisted in RavenDB, I get an exception:

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执行?

How to perform what I want using RavenDB ?

推荐答案

亚雷克, 你想要做的是:

Jarek, what you want to do is:

var q = session.Query<Question>();
foreach(var tag in tags)
{
    var currentTag = tag;
    q = q.Where(x=>x.Tags.Any(xTag=>xTag == currentTag));
}

这会给你所有的至少有所有这些标签的问题。

This will give you all the questions that have at least all those tags.

这篇关于查找对象至少包含从子使用RavenDB和LINQ的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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