skip to Main Content

I have a solution that builds. I’m now using Visual Studio 2022. But when I add this Linq statement, it fails to build, but shows no error.

Like this – this is the bottom left area of Visual Studio:

How I know the build fails with no error

    Dim playlistName as String = "blablabla"
    Dim objPlaylist = (From p In Playlists
                       Where p.Name = playlistName
                       Select p).FirstOrDefault()

Edit: I’ve now found the resolution and posted the answer. Everything below here is further detail about my project, but that had no bearing on what the source of the issue was in the end. Read as FYI only.


Playlists is a Property of type System.Collections.ICollection
It contains Playlist objects. I’ve shown the definition of these at the end of this post, below.

It still doesn’t build if I change Playlists to be a System.Collections.Generic.ICollection(Of Playlist), or System.Collections.Generic.IEnumerable(Of Playlist)

I found that if I change the Where clause to Where p.Name = "temp" then it will build! Also if I hardcode a playlist name that exists, the query will successfully find that playlist object.

The problem happens whether I use Name or Path in the Where clause. In both cases, an explicit string in the Where clause works, and a string variable fails to build.

So it won’t build because I’m using a variable in the Where clause of the Linq statement, and gives no build error to say why.

So what’s wrong with using a variable in the Where clause??? And if it’s wrong, why won’t it give me an error when it fails to build so I can have a clue!

More things I’m trying, and updates…

Of course Clean Solution and Rebuild all, and exit and re-open Visual Studio.

I’ve added a reference to System.Data.Linq.

I’ve added Imports System.Linq, but it tells me it’s unnecessary.

I’ve tried using .FirstOrDefault directly, however it still doesn’t build.

    Dim playlistName As String = "blablabla"
    Dim oPlaylist = TestPlaylists.FirstOrDefault(Function(p) p.Name = playlistName)

I tried opening a new solution to test this specific thing in isolation, however I’m only seeing (out of support) versions of .Net Framework. I’m going to upgrade some plumbing first. Going to upgrade to the latest Visual Studio and see if it persists. Ack! It’s still there in VS 2022.

Attaching the definition of the Playlists property, and the Playlist class (at the bottom of the post).

Now trying to target different (higher) framework versions, and will try again in a new solution after that.

I got to the point where it worked in a clean solution, even with Playlists being a Property, and the ViewModel being a NotificationObject, just like in the original project. And the same piece of code would work in the clean solution, but fail to build in the problem solution. Then I stumbled upon the root issue.

More code snippets

Playlists Property

        Private _playlists As System.Collections.ICollection
        Public Property Playlists() As System.Collections.ICollection
            Get
                Return _playlists
            End Get
            Set(ByVal value As System.Collections.ICollection)
                _playlists = value
                RaisePropertyChanged("Playlists")
            End Set
        End Property

Playlist class

<Serializable()>
Public Class Playlist
    Inherits SSUtil.CustomObjects.WPF.NotificationObject

    Public Path As String  ' path to the playlist on disk (relative to prefix)
    Public Songs As New System.Collections.SortedList(New CaseInsensitiveComparer)  ' list of songs in this playlist; just list of strings that index into main ColSongs
    Public HasChanged As Boolean
    Public ReadOnly Property Name() As String
        Get
            ' Just the name of the playlist, e.g. Metal.m3u
            Return JustName(Path)
        End Get
    End Property
    ... some more functions
End Class

2

Answers


  1. Chosen as BEST ANSWER

    Wow, so I found the offender. It had nothing to do with Linq, although that's when the problem manifested.

    I had an On Error Resume Next at the top of the function where I added the Linq query. That's the extent of it. If that line is commented out, there is no problem, and the solution builds successfully, and the Linq query works. With that line, build fails with no error.

    A second thing I discovered later is when I got another failed build with no errors, I checked the Output window. And there it showed the error that was being encountered! Fiding that saved me the pain I had to go through the first time around when my build was failing with no errors.

    I hope this helps someone working with an old project in the future. Cheers!


  2. If Playlists is System.Collections.ICollection then you need cast it to IColection<Playlist>

    Public Class X
        Private playlistName As String
    
        Public Sub OnClick()
            Dim objPlaylist As Playlist
    
            If String.IsNullOrEmpty(playlistName) Then
                Console.WriteLine("playlistName is empty")
                Exit Sub
            Else
                objPlaylist = Playlists.Cast(Of Playlist).FirstOrDefault(Function(p) p.Name = playlistName)
            End If
        End Sub
    
    End Class
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search