Skip to content

Instantly share code, notes, and snippets.

@felipeleusin
Last active October 26, 2016 13:32
Show Gist options
  • Save felipeleusin/f985ec5bce44f9d6fe3d4b5df27243ed to your computer and use it in GitHub Desktop.
Save felipeleusin/f985ec5bce44f9d6fe3d4b5df27243ed to your computer and use it in GitHub Desktop.
namespace RavenDB.WhereInTest
{
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
public class User
{
public string Id { get; set; }
public List<string> Roles { get; set; }
}
public class Users_Index : AbstractIndexCreationTask<User>
{
public Users_Index()
{
Map = users => from user in users
select new
{
user.Roles
};
Index(x => x.Roles, FieldIndexing.NotAnalyzed);
}
}
class Program
{
static void Main(string[] args)
{
using (var store = new EmbeddableDocumentStore() { RunInMemory = true }.Initialize())
{
IndexCreation.CreateIndexes(typeof(Users_Index).Assembly, store);
using (var session = store.OpenSession())
{
session.Store(new User()
{
Roles = new List<string>() { "Roles/User", "Roles/Admin" },
});
session.Store(new User()
{
Roles = new List<string>() { "Roles/User" },
});
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var query = session.Advanced.DocumentQuery<User, Users_Index>()
.WaitForNonStaleResults()
.UsingDefaultOperator(QueryOperator.And) // COMMENT THIS LINE TO MAKE IT WORK
.WhereIn("Roles", new[] { "Roles/Admin", "Roles/Manager" })
.Skip(0)
.Take(20)
.ToList();
Console.WriteLine($"Found {query.Count} users.");
}
}
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment