LINQ查询返回Ienumerable

时间:2015-10-14 15:40:57

标签: vb.net linq

我有以下两个班级

Public Class LookupsModel
Implements IEnumerable(Of LookupModel)

Public _LookupModel() As LookupModel

Public Sub New(pArray As ArrayList)
    _LookupModel = New LookupModel(pArray.Count - 1) {}

    Dim i As Integer
    For Each l As LookupModel In pArray
        _LookupModel(i) = l
        i += 1
    Next
End Sub

Public Function GetEnumerator() As IEnumerator(Of LookupModel) Implements IEnumerable(Of LookupModel).GetEnumerator
    Return New LookupEmum(_LookupModel)
End Function

Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
    Return New LookupEmum(_LookupModel)
End Function

Public Property Heading1 As String
Public Property Heading2 As String
Public Property Heading3 As String
Public Property Heading4 As String
Public Property Heading5 As String
Public Property Heading6 As String
Public Property CalledBy As String
Public Property ParmName1 As String
Public Property ParmName2 As String
Public Property ParmName3 As String
Public Property ParmName4 As String
Public Property ParmName5 As String
Public Property ParmName6 As String

Public Property ValueFieldGetter() As Func(Of LookupModel, String)
Public Property DescriptionFieldGetter() As Func(Of LookupModel, String)

End Class

Public Class LookupModel

Public Sub New(ByVal Key As String, Optional ByVal Desc As String = Nothing, Optional Extra_1 As String = Nothing, Optional Extra_2 As String = Nothing, Optional Extra_3 As String = Nothing, Optional Extra_4 As String = Nothing)
    Me.Field1 = Key
    Me.Field2 = Desc
    Me.Field3 = Extra_1
    Me.Field4 = Extra_2
    Me.Field5 = Extra_3
    Me.Field6 = Extra_4
End Sub
Public Sub New()
    Me.Field1 = Nothing
    Me.Field2 = Nothing
    Me.Field3 = Nothing
    Me.Field4 = Nothing
    Me.Field5 = Nothing
    Me.Field6 = Nothing
End Sub

Public Property Field1 As String
Public Property Field2 As String
Public Property Field3 As String
Public Property Field4 As String
Public Property Field5 As String
Public Property Field6 As String

End Class

这是linq查询:

Dim lm As LookupsModel = GetLookupsModel(FieldID, lookup, LookupPage:=1, SearchField:=SearchField, SearchFields:=searchFields, SearchString:=String.Empty)

Dim lm2 As IEnumerable(Of LookupModel) = lm.Where(Function(p)     p.Field1.Contains("A"))

我试图查询LookupsModel并且只获取LookupModel.Field1以" A"开头的LookupModel集合。但是以下查询返回LookenModel的Ienumerable,而不是LookupsModel对象,并排除了这些项。我们如何实现这一点并获得一个LookupsModel对象?

1 个答案:

答案 0 :(得分:0)

您无法直接从IEnumerable(Of LookpupModel)投射到LookupsModel。您有几个选择:

  • 添加一个包含现有IEnumerable(Of LookupModel)
  • 的构造函数
  • Where的输出转换为ArrayList并使用现有的构造函数。

但首先是LookupsModel的重点是什么?您所做的只是包装LookupModel数组而不添加任何其他功能。为什么不坚持使用IEnumerable(Of LookupModel)