Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq query with multiple Contains/Any for RavenDB

Tags:

c#

linq

ravendb

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?

like image 879
CodingInsomnia Avatar asked Nov 17 '10 18:11

CodingInsomnia


People also ask

Can we use multiple where clause in LINQ?

Filter collections using Where clause in C#. A single query expression may have multiple where clauses.

What is any () in Linq?

The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.

What is contains in Linq C#?

The Linq Contains Method in C# is used to check whether a sequence or collection (i.e. data source) contains a specified element or not. If the data source contains the specified element, then it returns true else return false.

What is the data type for Linq query?

There is no single datatype for LINQ queries as it depends on the context. In your case you are selecting "Car" so the datatype is going to be IQueryable.


1 Answers

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.

like image 86
Rob Ashton Avatar answered Nov 11 '22 07:11

Rob Ashton