WhiteleyJ
WhiteleyJ

Reputation: 1691

How to query on the type of a nested object in RavenDB

Here's the setup, I've got an object that contains a list of events like so:

public class Order
{
    public string Name {get; set;}
    public List<OrderEvent> OrderEvents {get; set;}
    public bool IsComplete() => OrderEvents.Any(e => e.GetType() == typeof(OrderComplete));
}

public class OrderEvent
{
    public DateTime TimeStamp {get; set;}
}

public class OrderSubmitted : OrderEvent {...quantity ect...}
public class OrderPaidFor : OrderEvent {...amounts...}
public class OrderComplete : OrderEvent {...more stuff...}

Now I can dump this data into the database and pull it out and all is good, but how do I write a query to just get the completed orders without pulling all the orders client side and filtering there?

I've tried the following query but I've been told that I can't translate GetType like that.

Session.Query<Order>()
       .Where(o => o.Events.Any(e => e.GetType() == typeof(OrderComplete)))
       .ToList();

I'm pretty sure there's a good way to do this using JObjects and querying the $type property, but google and my efforts haven't come up with anything good yet.

Thanks.

Upvotes: 0

Views: 139

Answers (1)

Jens Pettersson
Jens Pettersson

Reputation: 1177

What you can do is make the IsComplete() function a read only property instead. That way it would serialize down to IsComplete: true/false in the stored document.

Then you should be able to query like this:

Session.Query<Order>()
   .Where(o => o.IsComplete)
   .ToList();

Hope this helps!

Upvotes: 1

Related Questions