tal952
tal952

Reputation: 992

RavenDB Collection "in" Collection query

I need to preform a query that check if a collection is in given collection, just like the regular in operation but for collections.

class Post
{
    public string[] Tags {get;set;}
}


session.Queury<Post>.Where(x=>x.Tags.in(new[]{".net","c#","RavenDB"})).ToList();

so if i have in my DB:

  new Post{Tags= new[]{"C#",".net"}};

it will be returned

but if i have:

  new Post{Tags= new[]{"C#",".net","SQLServer"}};

it will not be returned.

Update:

what i am trying to do is this:

  session.Query<Post>()
  .Where(x => x.Tags.All(y => y.In(new[] { "C#", ".net", "RavenDB" })))
  .ToList();

but i got System.NotSupportedException.

Upvotes: 2

Views: 353

Answers (2)

tal952
tal952

Reputation: 992

I manage to find a solution:

static void Main(string[] args)
{
    var sessionStore = new EmbeddableDocumentStore
    {
        RunInMemory = true,
        UseEmbeddedHttpServer = true,
        Conventions =
        {
            DefaultQueryingConsistency = ConsistencyOptions.AlwaysWaitForNonStaleResultsAsOfLastWrite
        }
    };

    sessionStore.Initialize();

    using (var session = sessionStore.OpenSession())
    {
        var allTags = new[] {"C#", ".net", "RavenDB", "Linux", "Mac"};
        var tagsCollection = new[] {"C#", ".net", "RavenDB"};

        var complementTagsCollection = allTags.Except(tagsCollection).ToList();

        session.Store(new Post
        {
            Tags = new List<string>{"C#",".net"}
        });

        session.SaveChanges();

        // Posts where all their tags are in tagsCollection
        var result = session.Query<Post>().Where(x => !x.Tags.In(complementTagsCollection)).ToList();
    }
}

Upvotes: 1

Ayende Rahien
Ayende Rahien

Reputation: 22956

The way IN works, it matches ANY of them. If you want to match all you have to do a separate check for each.

Upvotes: 0

Related Questions