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 
prevents -> prevented