1

I have an entity like the following:

class Person
{
    public ICollection<string> OptOut { get; set; }
    //other fields
}

My application allows the user to create dynamic mailing lists based on a well-known set of fields in Person.

The customers should be able to opt out of the different mailing lists, so OptOut might contain something like [ "Marketing", "Financial" ].

The query for a specific mailing list currently aggregates the filter expressions (using Queryable.Where) and RavenDB creates the needed indexes without problems.

I have been reading, and it seems neither of these constructs are supported:

people.Where(x => !x.OptOut.Contains(mailingListType));
people.Where(x => !x.OptOut.Any(o => o == mailingListType));
people.Where(x => x.OptOut.All(o => o != mailingListType));

How can I create the right query?

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154

1 Answers1

1

You need to create an index for this type of query.

In the index you should flatten your OptOut collection so you can create queries on it.

More on this here:

How to query items from nested collections in Raven DB?

EDIT

It seems that this can be answered with a simple LuceneQuery w/o having to explicitly create and index.

var users = session.Advanced
                   .LuceneQuery<Person>()
                   .Where("OptOut:* AND -OptOut:" + newsLetterType)
                   .ToList();

EDIT 2

You will need to create this index:

from doc in docs.People
select new { OptOut = doc.OptOut.Count==0 ? "" : doc.OptOut}

To include person documents having no OptOut values.

It is frustrating how this is not available in the Query (typed client) but we can continue with that discussion on the mailing list.

Community
  • 1
  • 1
Raciel R.
  • 2,136
  • 20
  • 27