Problems with Except method
-
I have an XML doc that contains some file info (path, date created, etc.) and some media files in a folder with the same properties. Trying to devise some sort of way to compare the two lists and return a list of files that are not already described in the XML doc has been quite a challenge. First I get the files and their properties from the watched folder:
Dim mediaFiles = From file In fileinfo _
Where file.Extension = ".avi" Or file.Extension = ".mov" _
Or file.Extension = ".mkv" _
Or file.Extension = ".mp4" _
Or file.Extension = ".asf" _
Select fileThen the nodes from the XML doc that describe those files:
Dim showsInXML = From s In doc...<tvshow> _
Select sAfter that I compare the two lists, to get the ones that are the same:
Dim mediaOnFileSystemAndInXML = From mf In mediaFiles, sh In showsInXML _
Where mf.FullName = sh.<path>.Value _
Select mfSo far, so good. But after that when I want to 'invert' my selection (get only the files that are on disc, but not described in XML) with the following:
Dim mediaOnFileSystemAndNotInXML = mediaFiles.Except(mediaOnFileSystemAndInXML)
...I get the same ones as in the
mediaFiles
. As far as I can tell, I'm tellingExcept
to compare the same objects, so what gives? Already asked this in the VB.NET forum, but no luck. Hope you guys can help... -
I have an XML doc that contains some file info (path, date created, etc.) and some media files in a folder with the same properties. Trying to devise some sort of way to compare the two lists and return a list of files that are not already described in the XML doc has been quite a challenge. First I get the files and their properties from the watched folder:
Dim mediaFiles = From file In fileinfo _
Where file.Extension = ".avi" Or file.Extension = ".mov" _
Or file.Extension = ".mkv" _
Or file.Extension = ".mp4" _
Or file.Extension = ".asf" _
Select fileThen the nodes from the XML doc that describe those files:
Dim showsInXML = From s In doc...<tvshow> _
Select sAfter that I compare the two lists, to get the ones that are the same:
Dim mediaOnFileSystemAndInXML = From mf In mediaFiles, sh In showsInXML _
Where mf.FullName = sh.<path>.Value _
Select mfSo far, so good. But after that when I want to 'invert' my selection (get only the files that are on disc, but not described in XML) with the following:
Dim mediaOnFileSystemAndNotInXML = mediaFiles.Except(mediaOnFileSystemAndInXML)
...I get the same ones as in the
mediaFiles
. As far as I can tell, I'm tellingExcept
to compare the same objects, so what gives? Already asked this in the VB.NET forum, but no luck. Hope you guys can help...I'm not having any problems getting the Except to work like it should. Have you verified that the
mediaOnFileSystemAndInXML
has the items you expect to match in it? Perhaps you have mismatched cases in the filenames. -
I'm not having any problems getting the Except to work like it should. Have you verified that the
mediaOnFileSystemAndInXML
has the items you expect to match in it? Perhaps you have mismatched cases in the filenames.Thanks for your reply Gideon. I *think* the problem was that I was comparing items of
IEnumerable(Of System.IO.FileInfo)
andIEnumerable(Of <anonymous type>, System.IO.Fileinfo)
, as produced by the queries formediaFiles
andmediaOnFileSystemAndInXML
respectively. Anyway, I have since found what I think is a more elegant solution (at least in terms of length of code):Public Sub UpdateXML() Try Dim doc = XDocument.Load(My.Settings.xmlMedia) Dim newTvShows As New Collections.Generic.List(Of XElement) Dim showPathInXML = From s In doc...<tvshow> \_ Select s.<path>.Value For Each filePath As String In My.Computer.FileSystem.GetFiles(My.Settings.mediaFolder) Dim fileInfo As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(filePath) If CheckValidExtention(fileInfo.Extension) = False Then Continue For If showPathInXML.Any(Function(f) f = fileInfo.FullName) = False Then newTvShows.Add(<tvshow> <path><%= fileInfo.FullName %></path> <dateAdded><%= fileInfo.CreationTime %></dateAdded> <seen><%= "0" %></seen> <name><%= fileInfo.Name %></name> <deleted><%= "0" %></deleted> </tvshow> ) End If Next For Each ntvs As XElement In newTvShows doc.<torrss>.<tvshows>(0).Add(ntvs) Next doc.Save(My.Settings.xmlMedia) Catch ex As Exception ErrorNotifyer("Error updating XML.") End Try End Sub
I think the lesson here for me was that LINQ is a wonderful tool to be used sparingly. :)