2012-08-01 100 views
1

我有一個對象列表,我想搜索它以查看myobject.articleID是否與給定的articleID匹配。從我收集的使用.Find(的T)是這個最好的方式,但我有一些困難實施它。這裏有一些我到目前爲止的代碼:在列表中搜索對象

<WebMethod()> _ 
Public Function SetTagOnFavorite(ByVal articleID As Integer, ByVal tagtext As String, ByVal mobileGUID As String) As AddTagResult 
    Dim result As New AddTagResult 
    Dim userID As Long = GetUserIDByMobileGUID(mobileGUID) 


    If userID > 0 Then 
     Dim pageNum As Integer = 1 
     Dim pageLen As Integer = 500 'arbitrarily large number 
     Dim savedArticleList As New List(Of SimpleArticle) 
     savedArticleList = GetSavedArticles(mobileGUID, pageNum, pageLen) 

     If savedArticleList.Find(Function(m As SimpleArticle) m.articleID = articleID) 


      Dim lq As New lqDFDataContext 
      Dim var = lq.web_AddTagToArticle(userID, articleID, tagtext).ToList() 

      If var.Any() Then 
       Dim vRes = var.First() 
       result.articletagID = vRes.articletagID 
       result.newarticletag = vRes.newarticletag 
       result.newusertag = vRes.newusertag 
       result.usertagID = vRes.usertagID 
       result.resultinfo = "Success." 

      End If 
     End If 
    Else 
     result.resultinfo = STR_NoUserIDMostLikelyTheSessionTimedOut 
    End If 
    Return result 
End Function 

我得到的錯誤是,「SimpleArticle類型的值不能轉換爲布爾值」。

回答

4

因爲Find(Of返回找到的對象,您需要更改這一行:

If savedArticleList.Find(Function(m As SimpleArticle) m.articleID = articleID) 

If savedArticleList.Find(Function(m As SimpleArticle) m.articleID = articleID) IsNot Nothing 

,或者如果你需要找到的項目中,Find結果存儲在一個局部變量。

+0

這個工作,非常感謝你! – xxyyxx 2012-08-01 15:57:37

3

你可以進一步優化@ competent_tech的答案:

If savedArticleList.Any(Function(m) m.articleID = articleID))