Ciel
Ciel

Reputation: 17752

RavenDB, LINQ, select from string[] where array does not contain a given string

Given a very simple string[], I am trying to accomplish the following in a simple LINQ syntax with RavenDB.

public class Item {
   public string[] Tags { get; set; }
}
var list = new List<Item> {
  new Item { Tags = new string[] { "one", "two", "three" } },
  new Item { Tags = new string[] { "one", "two" } },
  new Item { Tags = new string[] { "one" } }
};

And then I can easily query this, like so;

var items = session.Query<Item>().Search( n => n.Tags, "one" ).Take(3).ToList();

This lets me easily get all of the items with a tag in their string[] that I want, but I want to do the opposite (obviously, for my use case the scenario is a lot more complicated, I am condensing it for this example).

I want to write a LINQ query that will work with Raven that will give me all of the objects that do not have a given tag. Like an 'inverted' search.

Contains() was removed from RavenDB, so I cannot use an inverse boolean on it. Until now, I just have to do a query, then use another LINQ command on the result, which I feel is highly inefficient. Does anyone know how to accomplish this?

I am also posting this on the RavenDB Google Groups Mailing List.

Upvotes: 3

Views: 1709

Answers (1)

Daniel Lang
Daniel Lang

Reputation: 6839

Session.Query<Item>()
       .Where(x => !x.Tags.Any(tag => tag == "one"))
       .ToList();

Upvotes: 5

Related Questions