2015-10-14 308 views
0

我有以下2類LINQ查詢返回的IEnumerable

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集合,其中LookupModel.Field1以「A」開頭。但是下面的查詢返回一個Ienumerable的LookupModel,而不是LookupsModel對象,並且這些項目被排除。我們如何做到這一點並獲得LookupsModel對象?

回答

0

您不能直接從IEnumerable(Of LookpupModel)轉換爲LookupsModel。您有幾種選擇:

  • 添加一個構造函數現有IEnumerable(Of LookupModel)
  • Where輸出轉換到ArrayList和使用您現有的構造函數。

但是LookupsModel首先是什麼意思?你所做的只是包裝一個LookupModel的數組,而不是添加任何附加功能。爲什麼不堅持使用IEnumerable(Of LookupModel)

+0

我想要一個LookupSmodel(複數),而不是一個LookupModel。 LookupsModel包含一個LookupModel數組。所以我想要一個LookupsModel,其中的數組中的所有LookupModel都符合條件 – jslumar

+0

@jslumar對不起,我錯過了這個非常微妙的區別。 –

+0

@D Stanley這個源代碼直接來自Microsoft Dynamics應用程序和他們的工程師,所以我不完全確定爲什麼封裝,但這是我必須使用的。我同意你的觀點,但我必須認爲他們有一些理由。可能是因爲查找類用於更多的事情,他們試​​圖使其具有普遍性。我確實喜歡將IEnumerable 轉換爲ArrayList,然後將其傳遞給構造函數的想法。我會盡力回覆你。 – jslumar