【发布时间】:2010-06-29 16:00:08
【问题描述】:
我正在尝试在 VB.NET 中使用嵌套的多行 lambda Function 并遇到错误。这是我的代码的样子:
cartItems = cartItems.Select(Function(ci) New With {.CartItem = ci, .Discount = discountItems.FirstOrDefault(Function(di) di.SKU = ci.SKU)})
.Select(Function(k)
If k.Discount Is Not Nothing Then
k.CartItem.Discount = minNumberOfItemsDiscounted * k.Discount.DiscountAmount
End If
Return k.CartItem
End Function)
这是很长的错误信息:
Error 1 Overload resolution failed because no accessible 'Select' can be called with these arguments:
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of <anonymous type>, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of <anonymous type>, Integer, TResult)'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of <anonymous type>, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of <anonymous type>, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': 'Is' operator does not accept operands of type 'Integer'. Operands must be reference or nullable types.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of <anonymous type>, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. C:\_Dev Projects\CMS2000\Components\NET\HBCatalogPromo\CatalogPromotion\CatalogPromotion.ConsoleTest\Module1.vb 88 21 CatalogPromotion.ConsoleTest
感觉我的语法有问题,因为我已通过在可能的情况下将Functions 行折叠到一行来修复其他行。但是,在这种情况下我不能这样做。
【问题讨论】:
-
你能粘贴整个声明吗?该错误似乎是由第一个“之前”的内容引起的 .Select...
-
我不认为你真的想要 Select - 或者更确切地说 你在做什么用它的副作用?!?!?
-
@Reed 我粘贴在第一行。我将其省略的原因是因为第一行本身不会引发错误。
-
请发布更多上下文。 cartItems 和 discountItems 列表中有哪些商品,它们有哪些字段?