Subquerying RealmResults

Issue #594 reports a wrong cast. The cast is wrong in the case where a `RealmResults` is queried using `where()`. This PR fixes the bug and adds unit tests to both plain queries and link queries.

@bmunkholm @cmelchior @emanuelez 
add comment to what you try to test.