The issue is not so much that the code is "inelegant", but that the data structure is not well adapter to your query. Perhaps you should rethink the data structure. Or, if you can't and if you have to do that often, then add a lookup table with the first names as the key and a list of records as a value. This will take some time to build but make queries faster and easier thereafter.