Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB9.0 .NET VB.NET
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'
 
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.
 
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:
 
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 11-Jun-12 2:12am
MarqW3.9K
Edited 11-Jun-12 2:50am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See Edit 2 - You apparently can't call a .WHERE inside a "For Each In .Where" loop
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 499
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100