0

I'm having Drinks and those have a relation ship called Types.

Drinks can have many types and types can have many Drinks.

Now I want to create a query that gives me all Drinks that are of type "sweet". Thats easy but now I'm having array of types and I want a query that gives me all Drinks that matches ANY of the types in the array.

I'm now working around this for my countries like this:

  List<String> filterCountries = mGson.fromJson(prefsUtil.filterCountries(), mType);
        if(filterCountries != null) {
            realmQuery.beginGroup();

            for (int cursor = 0; cursor < filterCountries.size(); cursor++) {
                String country = filterCountries.get(cursor);
                realmQuery.equalTo("country.name", country);
                if (cursor != (filterCountries.size() - 1)) {
                    realmQuery.or();
                }
            }
            realmQuery.endGroup();
        }

But I think this is an ugly solution. But I don't find anything to do this?

user1007522
  • 7,858
  • 17
  • 69
  • 113
  • You're probably thinking of a LINQ equivalent for Java: http://stackoverflow.com/questions/18154023/best-way-or-similar-lambda-expression-or-linq-on-android-eclipse - not sure what the current state of that is on Android – Morrison Chang Oct 23 '15 at 19:39
  • I think the `realmQuery.beginGroup();` and `realmQuery.endGroup();` are not necessary. Beside that, it looks OK. Why do you think it is ugly? – beeender Oct 27 '15 at 14:19
  • Because in iOS I can do this much shorter with the keyword ANY and now I need to write it like this. But thanks :-) – user1007522 Oct 30 '15 at 08:22

1 Answers1

1

you can iterate through an array like so:

String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
    //Do something
}

an if statement to check whether that array item is what you are looking for and add it to a 'results' array

Will Evers
  • 934
  • 9
  • 17
  • Thats not what I'm looking for. I want all drinks that have types Sweet, Fruity for example. So I want a query that gives me all drinks with those types. But this is variable thats why my types are in an array the user can choose this. – user1007522 Oct 23 '15 at 19:06
  • Yeah so go through all of the drinks in the array, check each one's type and add them to to a new array that contains the results you are looking for. – Will Evers Oct 23 '15 at 20:13
  • Thats a solution of n^2 worst case. There must be a better way? – user1007522 Oct 24 '15 at 07:48
  • Im sure there is a more efficient way but this method will work nonetheless – Will Evers Oct 24 '15 at 23:17