Projection from javascript object (C# Dictionary)

22 views
Skip to first unread message

Ivan Montilla

unread,
Nov 14, 2019, 8:04:40 AM11/14/19
to RavenDB - 2nd generation document database
In this query:

var query =
    from customer in session.Query<Individual>()
    where customer.Id == $"individuals/{id}"
    select new PhoneListModel
    {
        CustomerName = customer.Name,
        Phones =
            from phone in customer.Phones
            select new PhoneModel
            {
                Label = phone.Key,
                Prefix = phone.Value.CountryPrefix,
                Phone = phone.Value.Value
            }
    };

Phones is a Dictionary<string, Phone> in the example.

The generated query is this:

from Individuals as customer
where id() = $p0
select
{
    CustomerName : customer.Name, 
    Phones : customer.Phones.map(function(phone)
    {
        return
        {
            Label:phone.Key,
            Prefix:phone.Value.CountryPrefix,
            Phone:phone.Value.Value
        };
    })
}
limit $p1, $p2


Of course, in the stored document, Phone is a JavaScript object, so it is not stored with Key and Value words from KeyValuePair:

"Phones": {
    "Home": {
        "CountryPrefix": "0034",
        "Value": "612345678"
    },
    "Work": {
        "CountryPrefix": "0034",
        "Value": "961234567"
    }
},

Any idea how I can get the correct query?

Ivan Montilla

unread,
Nov 14, 2019, 10:04:16 AM11/14/19
to RavenDB - 2nd generation document database
var query =
    from customer in data.Query<Individual>()
    where customer.Id == $"individuals/{individualId}"
    select new
    {
        Name = customer.FirstName,
        Phone =
            (from phone in customer.Phones
            where phone.Key == phoneLabel
            select phone).FirstOrDefault()
    };

Similar query generated,

Egor Shamanaev

unread,
Nov 14, 2019, 10:30:29 AM11/14/19
to rav...@googlegroups.com
I getting other generated query then yours.
What are your class models? What is the type of  PhoneListModel.Phone ? 

I found a bug when I was trying to reproduce your issue
https://issues.hibernatingrhinos.com/issue/RavenDB_14269 

Please see the test I made 
https://github.com/ravendb/ravendb/pull/9883/files#diff-04304da7b77a3cf96147de22bae67d43  

--
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/320c5387-aa6f-4257-b5d0-a3b4a022a774%40googlegroups.com.

Ivan Montilla

unread,
Nov 14, 2019, 11:04:18 AM11/14/19
to RavenDB - 2nd generation document database
What are your class models? What is the type of  PhoneListModel.Phone ? 

class PhoneModel
{
   
public string Label { get; set; }
   
public string Prefix { get; set; }
   
public string Value { get; set; }
}

class PhoneListModel
{
    public string CustomerName { get; set; }
    public IEnumerable<PhoneModel> Phones { get; set; }
}


I found a bug when I was trying to reproduce your issue
https://issues.hibernatingrhinos.com/issue/RavenDB_14269 
404 Not found ;( 
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

Grisha Kotler

unread,
Nov 14, 2019, 11:33:30 AM11/14/19
to rav...@googlegroups.com

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/917317e9-ed95-4734-8cb3-4b22a84530ed%40googlegroups.com.

Ivan Montilla

unread,
Nov 14, 2019, 11:40:34 AM11/14/19
to RavenDB - 2nd generation document database
Reply all
Reply to author
Forward
0 new messages