Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ Query is throwing an error

Posted on 2011-03-16
10
Medium Priority
?
479 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
[X]
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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 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