Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB: How to query with multiple search terms

Tags:

ravendb

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?

like image 809
K Ronning Avatar asked Oct 26 '11 07:10

K Ronning


1 Answers

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();
}
like image 95
Thomas Freudenberg Avatar answered Oct 16 '22 15:10

Thomas Freudenberg