RE: [RavenDB] How to query an array of objects ?

927 views
Skip to first unread message

Kijana Woodard

unread,
Oct 25, 2014, 9:01:25 AM10/25/14
to rav...@googlegroups.com
Use In instead of Contains.
You'll need using Raven.Linq;

From: Nima Ha
Sent: ‎10/‎25/‎2014 4:19 AM
To: rav...@googlegroups.com
Subject: [RavenDB] How to query an array of objects ?

Here's a Person object :

public class Person
{
       public string FirstName{get;set;}
       public string LastName{get;set;}
       public string[] NickNames{get;set;} 
}

Suppose that we want to query all the persons with a certain nickname.("Tough Guy" for example)
I thought I could write it like this :

var query=session.Query<Person>().Where(p=>p.NickNames.Contains("Tough Guy"));

but this statement generates an empty query. 
I know I could use Any() but the problem is that I want to translate a string into a Linq query (For example : FirstName.Equals("John") AND NickNames.Contains("Tough Guy")) using Dynamic Linq Library and this library doesn't support extension methods such as Any ( unless I modify its source which I think it would be the last option)

So the question is how I can search for Though Guys in a list of documents ? :)

--
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.
For more options, visit https://groups.google.com/d/optout.

Nima Ha

unread,
Oct 25, 2014, 9:40:47 AM10/25/14
to rav...@googlegroups.com
I don't think that IN is a suitable method in this case or I am doing something wrong ?
The problem is I have a field in my document that is an array For example :

NickName :
[
"Though Guy","Lefty","Cute","Handsome"
]

All I want to do is to be able to get all people with "Tough Guy" nick name:
For example :

Query<Person>().Where(p=>p.NickNames.Any(x=>x=="Tough Guy"));

Which is translated to :

NickNames : "Though Guy"

I'd like to be able to do the same using a query like this :

Query<Person>().Where(p=>p.NickNames.Contains("Tough Guy"));

Which is not working (returns an empty query)


Kijana Woodard

unread,
Oct 25, 2014, 12:09:16 PM10/25/14
to rav...@googlegroups.com
iirc: "Tough Guy".In(p.NickNames)

From: Nima Ha
Sent: ‎10/‎25/‎2014 8:40 AM
To: rav...@googlegroups.com
Subject: Re: [RavenDB] How to query an array of objects ?

Tom Allard

unread,
Oct 25, 2014, 1:56:15 PM10/25/14
to rav...@googlegroups.com


You don't need in in this case,  you just need to do equals, == (+ you need to change NickNames to string instead of an array of string). This is because everything in lucene  (your indexes) is a simply stored as string.

-Tom

Oren Eini (Ayende Rahien)

unread,
Oct 26, 2014, 3:13:07 AM10/26/14
to ravendb
var query=session.Query<Person>().Where(p=>p.NickNames.Any(n => n == "Tough Guy"));


Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 

Reply all
Reply to author
Forward
0 new messages