Solved

LINQ Query is throwing an error

Posted on 2011-03-16
10
464 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

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 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 63

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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