发现其中包含由子集使用RavenDB和LINQ的至少一种元素的对象 [英] Finding objects which contains at least one element from subset using RavenDB and LINQ

查看:164
本文介绍了发现其中包含由子集使用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>() {"a"} };
var q2 = new Question("q2") { Tags = new List<string>() {"b"} };
var q3 = new Question("q3") { Tags = new List<string>() {"a", "b", "c"} };
var q4 = new Question("q4") { Tags = new List<string>() {"a", "b"} };
var q5 = new Question("q5") { Tags = new List<string>() {"z"} };
var q6 = new Question("q6");
var questions = new List<Question>() {q1, q2, q3, q4, q5, q6};

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

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

string[] tags = new[] {"a", "b"};

我期望的 Q1 Q2 Q3 Q4 即可返回。我用它来获得所需结果的查询是:

I expect q1, q2, q3 and q4 to be returned. The query which I use to get desired result is:

var questions = DocumentSession.Query<Question>().AsQueryable();
questions = GetQuestionsToContainingAtLeastOneTagFromSubset(questions, tags)
// some other query restrictions
var result = questions.ToList(); // (**)

这假设强加在我收集的限制的功能如下:

The function which suppose to impose restrictions on my collection is following:

private IQueryable<Question> GetQuestionsToContainingAtLeastOneTagFromSubset(IQueryable<Question> questions, IEnumerable<string> tags)
{
    var result = new List<Question>();
    foreach (var tag in tags)
    {
        var currentTag = tag;
        var resultForTag = questions.Where(x => x.Tags.Any(xTag => xTag == currentTag));
        result = result.Concat(resultForTag).ToList();
     }
     return result.GroupBy(x => x.Id).Select(grp => grp.First()).AsQueryable();
}

我觉得这是非常低效的。我想避免使用 .ToList()提供的函数中。据我了解这个 .ToList() EX pressions查询RavenDB并返回我的部分结果(顺便说一句:我是吧?)。这是效率不高。我只是想强加在所提供的功能的限制,并执行查询后,所有的限制始终存在。该(**)的地方是为我好,以发送批量RavenDB检索查询结果。

I think this is highly inefficient. I'd like to avoid using .ToList() inside the provided function. As I understand this .ToList() expressions queries the RavenDB and returns me partial results (BTW: am I right ?). This is not efficient. I only want to impose restrictions in the provided function, and execute the query after all of the restrictions are imposed. The (**) place is good for me to sent the batch to RavenDB retrieve query result.

如何提高呢?

推荐答案

您可以查询Lucene来获得相匹配侑标签阿雷阵列像这样的标签问题:

You can query lucene to get the questions with tags that match yor tag aray array something like this:

string[] tags = new[] { "a", "b" };
string queryRange = "(" + string.Join(" OR ", tags) + ")";

var res = session.Advanced.LuceneQuery<Question>()
               .Where("Tags:" + queryRange).ToList();

请注意,此查询整个索引的问题,而不是一个子集。但我认为你可以追加 - 我想 - 要查询字符串EX pression。 见Lucene的文档获取更多关于这种类型的查询: 的http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html

Note that this queries the entire indexed questions, not a subset. But I think you can append - I GUESS - to the query string expression. See Lucene docs for more about these kind of queries: http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html

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

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