使用Lambda .Where()时出现问题 [英] Problem using Lambda .Where()

查看:95
本文介绍了使用Lambda .Where()时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些代码上遇到了一个奇怪的问题.如果我使用此代码,它将在那里的custParts.Where(Function(cv),当机,错误:
委托给实例方法不能具有空的"this"

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



但是,如果我将lambda放入变量中,它似乎可以正常工作.



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



有谁知道为什么要这么做吗?以及为什么我不能只为Ror Each对抗我的Lambda?

谢谢


奇怪的是,这有效:



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

Thanks


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




好的,我实际上已经知道这是什么了.我失败代码的代码粘贴实际上错过了引起错误的位(尽管我确定我只是直接从源代码进行了C/P操作) ).所以我已经更新了.

问题是尝试在For的每个头中做第二个".Where".



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.

推荐答案

请参见编辑2-您显然不能调用.在"For Each In .Where"循环内的位置
See Edit 2 - You apparently can''t call a .WHERE inside a "For Each In .Where" loop


这篇关于使用Lambda .Where()时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆