Solved

LINQ Query is throwing an error

Posted on 2011-03-16
10
448 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
 
LVL 62

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 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 62

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now