0

In my application, there is a dictionary that stores objects of a custom type representing journal article authors. What I want to do is query this dictionary to extract a list of unique last name-first initial combinations. My query looks like this:

Dim LstNmFrstInits = Authors.Select(Function(kvp) New With {.LName = kvp.Value.AuthorLastName, .FInitial = kvp.Value.AuthorFirstName.First()}).Distinct()

When I attempt to iterate through LstNmFrstInits, the application throws an InvalidOperationException. I believe the problem happens when I call the First() method on the first name. I do not want to retrieve the entire first name; I only want the first initial.

user667118
  • 67
  • 10
  • Is `AuthorFirstName` a `String`? If so, you can simply get the first letter by using the indexer: `AuthorFirstName(0)` (just make sure it is not empty). – Styxxy Jun 03 '13 at 22:36
  • Thanks for the suggestion. It's not throwing the error, but now I'm facing a different problem in that the Distinct does not appear to be working. I wanted to use Linq to make the code more succinct, but I don't think this is worth it--I'll just loop through and add records to a new collection to get the list I want. – user667118 Jun 03 '13 at 23:19
  • Try using instead of an anonymous type, an actual type in which you implement the `GetHashCode` and `Equals` methods. The `Distinct` method should work then just fine. – Styxxy Jun 04 '13 at 10:00

0 Answers0