VB.NET的LINQ查询问题

时间:2013-03-15 19:58:47

标签: vb.net linq

我需要修改一个使用LINQ查询的函数,通过为每个项添加额外的布尔值来返回数据集合,所以为了简单起见,我决定将返回类型更改为{{1}的列表}。但是我有一个错误,我无法解决,或者很遗憾地理解。

原始版本或功能如下:

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

我在C#中做了很多LINQ查询,但在VB中没有那么多,所以很可能是我对语法的理解。我尝试了几种变体但没有成功,我可以将其转换为匿名类型而不是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. ,但这不适合此实现。

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

1 个答案:

答案 0 :(得分: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