用于 RavenDB 的具有多个 Contains/Any 的 Linq 查询

发布于 2024-10-02 14:42:42 字数 686 浏览 1 评论 0原文

我有一个包含“标签”列表的文档类。类似于:

class Item {
  string Name { get; set; }
  List<string> Tags {get; set;}
}

现在我想为 RavenDB 创建一个查询,它可以为我提供按标签列表过滤的所有项目。使用实体框架时,我设法通过以下方式做到这一点:

var query = GetQueryable();
foreach (var tag in tags)
{
   query = query.Where(i => i.Tags.Contains(tag));
}

但是,这似乎不适用于 RavenDB,很可能是因为不支持 Contains.. 我也尝试使用 Any 重写它,(Where(i => i.Tags.Any(t=>t == tag))) 但这给了我一个奇怪的例外:

Unable to cast object of type
'System.Linq.Expressions.PrimitiveParameterExpression`1[System.String]'
to type 'System.Linq.Expressions.MemberExpression

有什么好主意吗?我这样做完全错误吗?

I have a document class that contains a list of "tags". Something like:

class Item {
  string Name { get; set; }
  List<string> Tags {get; set;}
}

Now I would like to create a query for RavenDB that hands me all items filtered by a list of tags. When using Entity Framework I managed to do this by something like this:

var query = GetQueryable();
foreach (var tag in tags)
{
   query = query.Where(i => i.Tags.Contains(tag));
}

However, this doesn't seem to work with RavenDB, most likely because Contains isn't supported.. I've also tried rewriting it using Any, (Where(i => i.Tags.Any(t=>t == tag))) but that gives me a strange exception:

Unable to cast object of type
'System.Linq.Expressions.PrimitiveParameterExpression`1[System.String]'
to type 'System.Linq.Expressions.MemberExpression

Any great ideas? Am I doing this completely wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2024-10-09 14:42:42

Contains 确实尚未受支持(也许应该是,但这完全是另一回事 - 我们只是在需要时才真正添加对各种运算符的支持)

至于针对 Any 的多个查询,我假设您正在尝试执行动态数据并且您想要实现类似的目标

"X OR Y OR Z"

这是一个棘手的问题,默认情况下 LINQ 提供程序将使用 AND 聚合这些多个 WHERE 子句,因此您的示例看起来像

"X AND Y AND Z"

Which 显然永远不会出现这种情况。

对于这个问题,您最好的选择是下拉至 Lucene 查询(至少目前如此)并执行以下操作:

var results = s.Advanced.LuceneQuery<Item>()
                   .Where(string.Format("Tags,:({0})", string.Join(" OR ", tags))); 

有意义吗?

上面的查询看起来像

"Tags,:(X OR Y OR Z)"

注意:“标签”,通知 RavenDB Tags 是一个数组

好的,[编辑]!

获得您真正想要的内容的最简单方法是按照以下方式执行操作

                new IndexDefinition<Item, Item>()
                {
                    Map = docs => from doc in docs
                                  select new
                                  {
                                      Tags = doc.Tags
                                  },
                    Indexes = {{ x => x.Tags, FieldIndexing.Analyzed }}
                }.ToIndexDefinition(store.Conventions));

然后要查询您的 and ,您可以执行如下操作:

                var results = s.Advanced.LuceneQuery<Item, WhateverYouCalledThatIndex>()
                   .Where(string.Format("Tags:({0})", string.Join(" AND ", tags)));

现在,需要注意的事情

       Tags = doc.Tags

将把整个数组序列化为一个巨大的斑点,因为它只是适用于此示例的字符串。

我正在寻找更好的方式来表达这一点,我们不太可能想出一种类似 LINQ 的方式来做到这一点,因为它并没有真正很好地映射 - 但它一个可行的答案:)

我想我很想至少能够做到

  Map = docs => from doc in docs
                                  select new
                                  {
                                      Tags = String.Join(" ", doc.Tags)
                                  },

(这行不通,所以不要尝试),但它对于你想要实现的目标更加明确。

Contains is indeed not yet supported (Perhaps it should be, but that's another matter entirely - we only really add support for various operators when its asked for)

As for multiple queries against Any, I assume you're trying to do dynamic data and you want to achieve something like

"X OR Y OR Z"

That's a tricky one, and the LINQ provider by default will aggregate those multiple WHERE clauses with AND, so your example looks like

"X AND Y AND Z"

Which will obviously never be the case.

Your best option for this one is to drop down to the Lucene query (at least for now) and do something like this:

var results = s.Advanced.LuceneQuery<Item>()
                   .Where(string.Format("Tags,:({0})", string.Join(" OR ", tags))); 

Make sense?

The query above will look something like

"Tags,:(X OR Y OR Z)"

Note: "Tags," informs RavenDB that Tags is an array

Okay, [edit]!

The easiest way to get what you actually want is to do something along these lines

                new IndexDefinition<Item, Item>()
                {
                    Map = docs => from doc in docs
                                  select new
                                  {
                                      Tags = doc.Tags
                                  },
                    Indexes = {{ x => x.Tags, FieldIndexing.Analyzed }}
                }.ToIndexDefinition(store.Conventions));

Then to query for your ands, you can do something like this:

                var results = s.Advanced.LuceneQuery<Item, WhateverYouCalledThatIndex>()
                   .Where(string.Format("Tags:({0})", string.Join(" AND ", tags)));

Now, things to be aware of

       Tags = doc.Tags

Will serialize that entire array into one giant blob, as it's just strings that will work for this example.

I am looking at better ways of expressing this, it is unlikely that we'll come up with a LINQ-ish way of doing this, as it doesn't really map across very well - but it is an answer that will work :)

I think I'd quite like to be able to at least do

  Map = docs => from doc in docs
                                  select new
                                  {
                                      Tags = String.Join(" ", doc.Tags)
                                  },

(This won't work so don't try it), but it is a bit more explicit about what you want to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文