0

我需要修改一个函数,该函数使用 LINQ 查询通过为每个项目添加一个额外的布尔值来返回数据集合,因此为简单起见,我决定将返回类型更改为KeyValuePairs. 但是,我有一个我无法解决或不幸理解的错误。

原版或功能如下:

Private Function GetSelectedExtractors() As List(Of ExtractionMapping)

    Return _extractionSelections _
        .SelectMany(Function(x) x.ExtractionRoutineSelection) _
        .Where(Function(x) x.Selected) _
        .Select(Function(x) x.ExtractionMapping)

End Function

有错误的新版本如下:

Private Function GetSelectedExtractors() As List(Of KeyValuePair(Of ExtractionMapping, Boolean))

    Return _extractionSelections _
        .SelectMany(Function(x) x.ExtractionRoutineSelection) _
        .Where(Function(x) x.Selected) _
        .Select(Function(x) New KeyValuePair(Of ExtractionMapping, Boolean) _
                     (x.ExtractionMapping, x.DeleteExistingInstances))

End Function

我收到的错误是这样的:

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 ExtractionRoutineSelection, 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 ExtractionRoutineSelection, Integer, TResult)'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of ExtractionRoutineSelection, 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 ExtractionRoutineSelection, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': 'DeleteExistingInstances' is not a member of 'Domain.ProductionWizard.ExtractionRoutineSelection'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of ExtractionRoutineSelection, 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# 中做了很多 LINQ 查询,但在 VB 中没有那么多,所以这很可能是我对语法的理解。我尝试了几种变体都没有成功,我可以将它转换为匿名类型而不是 a KeyValuePair,但这不适合这种实现。

任何解决方案或提示将不胜感激。

4

1 回答 1

0

看起来x.DeleteExistingInstances无效:

'DeleteExistingInstances' is not a member of Domain.ProductionWizard.ExtractionRoutineSelection'

修复它,它应该可以按您的意愿工作。

但是,我建议使用Tuple而不是KeyValuePair

Private Function GetSelectedExtractors() As List(Of Tuple(Of ExtractionMapping, Boolean))

    Return _extractionSelections _
        .SelectMany(Function(x) x.ExtractionRoutineSelection) _
        .Where(Function(x) x.Selected) _
        .Select(Function(x) Tuple.Create(x.ExtractionMapping, x.DeleteExistingInstances))
        .ToList()

End Function
于 2013-03-15T20:14:07.367 回答