Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Problems with Except method

Problems with Except method

Scheduled Pinned Locked Moved LINQ
csharpxmlhelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gGregor83
    wrote on last edited by
    #1

    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 file

    Then the nodes from the XML doc that describe those files:

    Dim showsInXML = From s In doc...<tvshow> _
    Select s

    After 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 mf

    So 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 telling Except to compare the same objects, so what gives? Already asked this in the VB.NET forum, but no luck. Hope you guys can help...

    G 1 Reply Last reply
    0
    • G gGregor83

      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 file

      Then the nodes from the XML doc that describe those files:

      Dim showsInXML = From s In doc...<tvshow> _
      Select s

      After 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 mf

      So 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 telling Except to compare the same objects, so what gives? Already asked this in the VB.NET forum, but no luck. Hope you guys can help...

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • G Gideon Engelberth

        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.

        G Offline
        G Offline
        gGregor83
        wrote on last edited by
        #3

        Thanks for your reply Gideon. I *think* the problem was that I was comparing items of IEnumerable(Of System.IO.FileInfo) and IEnumerable(Of <anonymous type>, System.IO.Fileinfo), as produced by the queries for mediaFiles and mediaOnFileSystemAndInXML 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. :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups