用VB.NET解决LINQ查询问题

用VB.NET解决LINQ查询问题,vb.net,linq,Vb.net,Linq,我需要修改一个函数,该函数使用LINQ查询通过为每个项添加额外的布尔值来返回数据集合,因此为了简单起见,我决定将返回类型更改为KeyValuePairs的列表。然而,我有一个错误,我无法解决,或不幸地理解 原始版本或功能如下所示: Private Function GetSelectedExtractors() As List(Of ExtractionMapping) Return _extractionSelections _ .SelectMany(Functio

我需要修改一个函数,该函数使用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中没有那么多,因此这可能是我对语法的理解。我尝试了几种变体但没有成功,我可以将其转换为匿名类型,而不是
KeyValuePair
,但这不适合此实现


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

看起来像
x。删除现有实例
无效:

'DeleteExistingInstances' is not a member of Domain.ProductionWizard.ExtractionRoutineSelection'
解决了这个问题,它应该可以按照你的意愿工作

但是,我建议使用而不是
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

编写问题时,
x.DeleteExistingInstances
是一个打字错误!但是,在修复时,我注意到我实际上在错误的类上添加了有问题的属性!我觉得自己像个傻瓜!另一方面,在这种情况下,为什么要选择元组?我确实使用元组,但通常当输出中有两个以上的对象时。只是对你的观点感兴趣。因为看起来你的数据之间并没有真正的关键价值关系。只有两个值,其中第一个值和第二个值都不是键。当然-你有一个更大的图景,所以它可能真的是关键价值。我不认为只有那个方法才是这样。谢谢你的解释,我想我已经使用
KeyValuePair
s这么长时间了,我一直忘了
Tuple
s可以用于很多类似的情况!