Sean Forman
Sean Forman

Reputation: 400

Could not understand expression - RavenDB

I have a set of deals, each of which have a list of User Ids that are allowed to access the deal.

Simply querying Raven with

var theId = 6;
var deals = session.Query<Deal>().Where(x => x.UserIds.Contains(theId)).ToList();

Where the Deal class looks like

public class Deal {
    public Int32 Id { get; set; }
    public List<Int32> UserIds = new List<Int32>();
}

Results in the exception

Could not understand expression: .Where(x => x.UserIds.Contains(value(Deals_Manager.Controllers.DealsController+<>c__DisplayClass4).theId))

Inner exception

{"Expression type not supported: value(Deals_Manager.Controllers.DealsController+<>c__DisplayClass4).theId"}

What's the issue?

Upvotes: 0

Views: 1173

Answers (1)

David Boike
David Boike

Reputation: 18635

A Contains inside a Where is an extremely difficult expression tree to convert to the proper Lucene expression of UserIds:6, let alone when you introduce a closure to bring the outside variable into the lambda expression. (The closure is what's making your exception look so goofy, by the way.)

It's further complicated by the fact that your model is a List which has its own special version of Contains which is different than the IEnumerable version.

Try this on for size instead:

var theId = 6;
var deals = session.Query<Deal>()
    .Where(x => x.UserIds.Any(id => id == theId))
    .ToList();

For any expression tree that Raven cannot understand, just try to think of it in more primitive terms.

Upvotes: 4

Related Questions