30

My entity is:

class Resource
{
    string Name;
    string EmployeeId;
}

How do I query for resources of multiple employees? I tried this:

Resource[] FindResourcesByEmployees(string[] employeeIds)
{
    return this.Session.Query<Resource>()
        .Where(r => employeeIds.Contains(r.EmployeeId))
        .ToArray();
}

However that gives me NotSupportedException: Method not supported: Contains. Then I tried the following method:

Resource[] FindResourcesByEmployees(string[] employeeIds)
{
    return this.Session.Query<Resource>()
        .Where(r => employeeIds.Any(v => v == r.EmployeeId))
        .ToArray();
}

That throws NotSupportedException: Expression type not supported: System.Linq.Expressions.TypedParameterException.

In SQL it would be something like:

SELECT * FROM resource WHERE employeeid IN (1, 2, 3)

My question is, how do I perform this query in RavenDB?

Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
K Ronning
  • 531
  • 4
  • 10
  • This post may help you out http://stackoverflow.com/questions/4207739/linq-query-with-multiple-contains-any-for-ravendb – JonVD Oct 26 '11 at 08:01
  • 1
    Nope, that case regards the entity itself containing a collection. In my case only the query contains a collection, while the entity contains no collections. – K Ronning Oct 26 '11 at 12:23

1 Answers1

65

You can use the In operator. If I remember correctly your code should look like this:

using Raven.Client.Linq;

Resource[] FindResourcesByEmployees(string[] employeeIds)
{
    return this.Session.Query<Resource>()
        .Where(r => r.EmployeeId.In<string>(employeeIds)))
        .ToArray();
}
Dr Rob Lang
  • 6,659
  • 5
  • 40
  • 60
Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44