Reputation: 531
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?
Upvotes: 30
Views: 5208
Reputation: 5078
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();
}
Upvotes: 65