How to writte a projection with relationships

39 views
Skip to first unread message

Ivan Montilla

unread,
Nov 15, 2019, 6:57:06 AM11/15/19
to RavenDB - 2nd generation document database
Hello,

In my model, I have individuals (individual customer) and businesses. Individuals can manage 0 to n businesses, and a business can be managed by one or more individuals.

Now, I'm trying to list all businesses of one individual, and I'm writting a projection like this:

string id = "1313-A";
var query = data.Query<Individual>()
    .Where(x => x.Id == $"individuals/{id}")
    .Include(x => x.BusinessesIds)
    .Select(x => new
    {
        Name = $"{x.FirstName} {x.LastName}",
        Businesses = data.Query<Business>()
            .Where(y => y.Id.In(x.BusinessesIds))
            .Select(y => new
            {
                Name = y.LegalName
            })
    });

I'm trying to perform a projection that get the individual name and its managed businesses in the same query, but I get an exception from Lambda2Js:

NotSupportedException: By default, Lambda2Js cannot convert custom instance methods, only static ones. `Query` is not static.

And the client cannot undestand the the LINQ expression.

Any idea how can I write this projection?

Arkadiusz Palinski

unread,
Nov 15, 2019, 7:29:20 AM11/15/19
to RavenDB - 2nd generation document database
RQL:


declare function output(i) {
    return {
        Name: i.Name
        Businesses : i.BusinessesIds.map(x => load(x).Name)
    }
}


from Individuals as i where id() = "individuals/1" select output(i)

C#

session.Query<Invdividual>().Where(x => x.Id == "individuals/1")
                        .Select(x => new {Name = x.Name, Businesses = x.BusinessesIds.Select(b => RavenQuery.Load<Businesses>(b).Name)})




--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/16fd7197-2190-4104-b897-3f63889667d5%40googlegroups.com.


--
Arkadiusz Paliński
Team Leader   /   Hibernating Rhinos LTD
Support:  sup...@ravendb.net
  

Ivan Montilla

unread,
Nov 15, 2019, 7:49:14 AM11/15/19
to RavenDB - 2nd generation document database
Thanks Arkadiusz
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

Ivan Montilla

unread,
Nov 15, 2019, 7:56:41 AM11/15/19
to RavenDB - 2nd generation document database
The final query is:

var query = data.Query<Individual>()
                .Where(x => x.Id == $"individuals/{id}")
                .Select(x => new BusinessListModel
                {
                    CustomerName = $"{x.FirstName} {x.LastName}",
                    Businesses = x.BusinessesIds.Select(y => new BusinessListModel.BusinessModel
                    {
                        Name = RavenQuery.Load<Business>(y).LegalName, // Here!
                        Document = RavenQuery.Load<Business>(y).Document // And here!
                    })
                })

The are any performance problem for use Load two times?

El viernes, 15 de noviembre de 2019, 13:29:20 (UTC+1), Arkadiusz Palinski escribió:
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

Arkadiusz Palinski

unread,
Nov 15, 2019, 9:54:12 AM11/15/19
to RavenDB - 2nd generation document database
You'll be better of by using let:

let c = RavenQuery.Load<Company>(o.Company)
Example:


To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/8f86ff6b-cd46-4a8a-b440-c9c20ebf6c31%40googlegroups.com.

Ivan Montilla

unread,
Nov 15, 2019, 9:58:06 AM11/15/19
to RavenDB - 2nd generation document database
I can't use let with Linq method syntax, but I can store in a variable before writting the query?.

Arkadiusz Palinski

unread,
Nov 15, 2019, 10:12:23 AM11/15/19
to RavenDB - 2nd generation document database
Yes, it requires to use query syntax - as shown in the example I sent you

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/173bec77-0ff6-4589-ba34-6abe87f57ac7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages