Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
5.00/5 (4 votes)
See more:
I've got a weird problem with some code. If I use this code, it crashes on there custParts.Where(Function(cv), with the error:
Delegate to an instance method cannot have null 'this'

VB
Dim custParts As CustomerPartNumbers = New CustomerPartNumbers() ' Inherits ComponentModel.BindingList(Of CustomerPartNumber), which is just another data class
Dim queryString As String = "Wallpaper"
custParts.Load("DummyCustomer")
If Not custParts Is Nothing Then
  Dim qS As String = queryString
  Dim cp As CustomerPartNumber
  For Each cp In custParts.Where(Function(cv) cv.CustomerPartNumber = qS)
    Dim cpPN As String = cp.PartNumber
    If prodResults.Where(Function(z) z.PartNumber = cpPN).Count = 0 Then
      prodResults.Add(New Hathaway.Library.Part(cpPN))
    End If
  Next
End If


However, if I put the lambda into a variable, it seems to work without a problem.

VB
Dim custParts As CustomerPartNumbers = New CustomerPartNumbers() ' Inherits ComponentModel.BindingList(Of CustomerPartNumber), which is just another data class
Dim queryString As String = "Wallpaper"
custParts.Load("DummyCustomer")
If Not custParts Is Nothing Then
  Dim qS As String = queryString
  Dim xxx As Object = custParts.Where(Function(cv) cv.CustomerPartNumber = qS)
  Dim cp As CustomerPartNumber
  For Each cp In xxx
    Dim cpPN As String = cp.PartNumber
  Next
End If


Does anybody have any ideas why it's doing this? and why I can't just for Ror Each against my Lambda??

Thanks

Edit:
Bizarrely, this works:

VB
Dim zxcustParts As CustomerPartNumbers = New CustomerPartNumbers()
zxcustParts.Load("DummyCustomer")
Dim zxqueryString As String = "Wallpaper"
For Each zxcustPart In zxcustParts.Where(Function(zxcp) zxcp.CustomerPartNumber = zxqueryString)
  Dim cpPN As String = zxcustPart.PartNumber
Next


Edit 2:
Okay, I've actually figured out what this is... My code paste of the failing code actually missed the bit which was causing the error (though I'm sure I just C/P'd straight from the source). So I've updated that.

The problem is trying to do a second ".Where" inside a For Each of the first.
Posted
Updated 23-Jun-20 3:26am
v3

See Edit 2 - You apparently can't call a .WHERE inside a "For Each In .Where" loop
 
Share this answer
 
The problem is that the item passed by the lambda expression is null... think about this

ITEM1
name:Guillermo
age:14

ITEM2
name:null
age:null

ITEM3
name:Jordan
age:18

now if I do my where:

var x = mylist.Where(w => w.name.Contains("Jor"))

the error will show, because the item name is null... I resolved it by using a test first as follow:

var x = mylist.Where(w => w.name != null && w.name.Contains("Jor"))

hope this helps!
 
Share this answer
 
Comments
Richard MacCutchan 23-Jun-20 10:41am    
Already answered eight years ago.
Guillermo Perez 23-Jun-20 10:47am    
yes, but the answer is wrong, the solution 1 says you can't use where inside a for loop, which is incorrect...
Richard MacCutchan 23-Jun-20 11:16am    
There are plenty of current questions that are waiting for answers, rather than resurrecting dead ones.
Guillermo Perez 23-Jun-20 11:24am    
dead ones? nobody replied this correctly (not even you!) and did happen to me yesterday... what's going on?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900