Question Get list of string from a list of objects

jrbilodeau

Member
Joined
Apr 29, 2009
Messages
7
Programming Experience
1-3
Hi,

I was wondering how I could go about getting a list of string from a list of objects, while removing duplicates

Here if my object

VB.NET:
Public Structure Movie
    Implements IComparable

    'Member variables
    Public WindowsMovieFilename As String
    Public PBOMovieFilename As String
    Public MovieSheet As String
    Public CoverArt As String
    Public Nfo As String
    Public Title As String
    Public Genres As List(Of String)


    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

        If TypeOf obj Is Movie Then
            Dim temp As Movie = CType(obj, Movie)
            Return Title.CompareTo(temp.Title)
        Else
            Throw New Exception("Invalid object type")
        End If

    End Function

End Structure

and here is what i tried


VB.NET:
'movies is a list(of Movie)
'get all genres from all movies and remove duplicate genres and store it in a list of strings
Dim genreList As New List(Of String)
genreList.Distinct(movies.FindAll(Function(m As Movie) m.Genres.FindAll(Function(g As String) g))).ToList()

The error that i get when trying to do this is Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'Boolean'.


I know that i can do a nested for each loop to iterate through movie and genres but i was trying to find a more efficient way of doing it.

Thanks
 
Dim genres = From m In movies From g In m.Genres Select g Distinct

genres here is an IEnumerable(Of String), you can convert it to a List(Of String) using the ToList function if needed.

Also change the Movie Structure to a Class, see Choosing Between Classes and Structures
 
Back
Top