Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ Query is throwing an error

Posted on 2011-03-16
10
Medium Priority
?
486 Views
Last Modified: 2012-05-11
I am attempting to use Linq to query a list but I keep getting an error I can't track down.  

This is the class that defines the custom object I am adding to the list:
Public Class SearchedPhoneNumber
    Private _id As New Integer
    Private _fullnumber As String
    Private _linenumber
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property FullNumber() As String
        Get
            Return _fullnumber
        End Get
        Set(ByVal value As String)
            _fullnumber = value
        End Set
    End Property
    Public Property LineNumber() As String
        Get
            Return _linenumber
        End Get
        Set(ByVal value As String)
            _linenumber = value
        End Set
    End Property
    Public Sub New()

    End Sub
End Class

Open in new window


This builds the list I intend to query:
Private Sub PopulatePhoneList()

        Dim getinfo = (From p In _context.PhoneNumbers _
                        Order By p.AreaCode, p.Exchange, p.LineNumber _
                        Select New SearchedPhoneNumber With {.ID = p.PhoneNumberID, _
                                                          .FullNumber = p.AreaCode + p.Exchange + p.LineNumber, _
                                                          .LineNumber = p.LineNumber}).ToList

        For Each g In getinfo
            _phonelist.Add(g)
        Next
        
    End Sub

Open in new window


To this point everything works fine. When I do this:
Private Sub PhoneSearch(ByVal sender As System.Object, ByVal e As System.Windows.Controls.PopulatingEventArgs)
        Dim searchstring As String = acbPhoneSearch.Text
            _phonesearch.Clear()
            Dim getPhoneNumbers = (From p In _phonelist _
                           Where p.LineNumber.StartsWith(searchstring) _
                           Select p)
            If getPhoneNumbers.Count < 15 Then
                For Each p In getPhoneNumbers
                    _phonesearch.Add(p)
                Next
            End If

End Sub

Open in new window


getPhoneNumbers never instantiates. I get "at solutionname.projectname._Closure$__38._Lambda$__19(SearchedPhoneNumber p) in filepath.xaml.vb:line 908
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()"

I use a very similar approach in another part of my app with no problem. Anyone know why this isn't working?
0
Comment
Question by:dev_ven
  • 4
  • 4
  • 2
10 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35148077
I think you are missing the .ToList() call. Try this:

Private Sub PhoneSearch(ByVal sender As System.Object, ByVal e As System.Windows.Controls.PopulatingEventArgs)
        Dim searchstring As String = acbPhoneSearch.Text
            _phonesearch.Clear()
            Dim getPhoneNumbers = (From p In _phonelist _
                           Where p.LineNumber.StartsWith(searchstring) _
                           Select p).ToList()
            If getPhoneNumbers.Count < 15 Then
                For Each p In getPhoneNumbers
                    _phonesearch.Add(p)
                Next
            End If

End Sub

Open in new window

0
 

Author Comment

by:dev_ven
ID: 35148216
wdosanjos:

Thanks for your quick response! ToList didn't help; same error.

0
 

Author Comment

by:dev_ven
ID: 35148501
More information: It seems to be the line "Where p.LineNumber.StartsWith(searchstring) _" that is causing the problem. When I remove it the errors disappear.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35148512
Hi dev_ven;

The query looks correct I can not see an error in it. The error states something about a Lambda but do not see a Lambda in the query. The error also makes reference to line 908 in a xaml file. Can you post that and the lines around it.

Fernando
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35148515
What line of code throws the exception?  The 'For Each'?
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35148533
Do all instances in _phonelist have LineNumber <> Nothing?
0
 

Author Comment

by:dev_ven
ID: 35148587
FernandoSoto:

We have something in common; I don't see the problem either! I am developing a flat spot on my head where I have been beating it against the wall... ;-)

Line 908 is actually the section I mention above ("Where p.LineNumber.StartsWith(searchstring) _") which is in the XAML code-behind.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35148673
Hi dev_ven;

Can you try to reproduce this into a test project and post it to the EE Stuff site. If you can follow these steps. I need to leave right now but will be back in a couple of hours and will look to see if any updates.

Zip the complete project and upload it. Please use the EE site below it will not give you issues with file types,

To upload a file in zip format to the Experts Exchange Stuff web site at http://www.ee-stuff.com follow these steps:

1.  Zip the files or project to be uploaded
2.  The go to http://www.ee-stuff.com
3.  If you are prompted to log in use the same username and password you use on the main site here
4.  At the top of the page click on "Expert Area" tab
5.  Then click on "Upload a new file" link
6.  Follow the instructions on the page
7.  After a successful upload post the link of the file in the question.

Fernando
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35148722
I'm not sure if that matters, but Private _linenumber does not have a type defined.
0
 

Author Comment

by:dev_ven
ID: 35148735
wdosanjos:

That did the trick! I modified the query that builds the original list of numbers to only include properly formatted phone numbers (i.e. 3 digit area code, 3 digit exchange, etc). I suspected at one point that my source data might be a problem but I could never isolate a specific record.

Thanks for everyone's help!

M
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question