Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

LINQ Query is throwing an error

Posted on 2011-03-16
10
461 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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