How to select first item of collection (array or dictionary)

15 views
Skip to first unread message

Ivan Montilla

unread,
Nov 19, 2019, 10:56:20 AM11/19/19
to RavenDB - 2nd generation document database
from customer in data.Query<Individual>()
where customer.Id == $"individuals/{individualId}"
select
    (from phone in customer.Phones
    where phone.Key == phoneLabel
    select new UpdatePhoneModel
    {
        Label = phone.Key,
        Prefix = phone.Value.CountryPrefix,
        Value = phone.Value.Value
    }).FirstOrDefault();

In this example, I'm trying to project only one item of `Phones` dictionary, but cannot be translated:

InvalidOperationException: Cannot understand how to translate customer.Phones.Where(phone => (phone.Key == value(Vadavo.IdServer.Areas.Admin.Controllers.IndividualController+<>c__DisplayClass9_0).phoneLabel)).Select(phone => new UpdatePhoneModel() {Label = phone.Key, Prefix = phone.Value.CountryPrefix, Value = phone.Value.Value}).FirstOrDefault()

Phones in database is stored as json object because is a dictionary on my .NET entity class. I'm trying to query by dictonary's key.

Any idea how can I write this query to be translated?

Aviv Rahmany

unread,
Nov 19, 2019, 12:15:59 PM11/19/19
to rav...@googlegroups.com
Hi ,

Projection with  `select (nested-query)` is not supported.

I've created a ticket for that:

You can achieve that by using `let`:

```
from customer in session.Query<Individual>()

where customer.Id == $"individuals/{individualId}"
let phone = customer.Phones.FirstOrDefault(p => p.Key == phoneLabel)

select new UpdatePhoneModel
{
   Label = phone.Key,
   Prefix = phone.Value.CountryPrefix,
   Value = phone.Value.Value
};
```

Regards

--
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/a6fe24c5-a768-4e17-99ad-faf9be51ef34%40googlegroups.com.


--
Aviv Rahmany
Core Team Developer   /   Hibernating Rhinos LTD
Mobile:  972-52-7039724
Sales:  sa...@ravendb.net
Skype:  aviv86
Support:  sup...@ravendb.net

Ivan Montilla

unread,
Nov 19, 2019, 12:21:54 PM11/19/19
to RavenDB - 2nd generation document database
Thank you!
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages