2011-06-27 47 views
0

.Find()メソッドを使用しようとしましたが正常に終了しました。しかし、私はFindAllを使って、 "柔軟な"キーワードにマッチするすべてのアイテムを受け取る方法を理解できません(私の場合、このキーワードはClassGuidと呼ばれています)。VB.Net List(of)オブジェクトの.FindAllを使用する方法は?

Public Class clsFindConnection 
Private Delegate Function ConMatchDelegate(ByVal con As PropertyConnection, ByVal ClassGuid As String) As Boolean 



    Public Function GetPropertyConnectionsByGuid(ByVal ClassGuid As String, ByVal LBaseConnections As List(Of PropertyConnection)) As List(Of PropertyConnection) 
     Dim Res As List(Of PropertyConnection) 
     Dim dl As New ConMatchDelegate(AddressOf ConnectionFromMatch) 
     Res = LBaseConnections.FindAll(dl)'<-- ERROR. Can not work because delegate is only using a single item. 
     Return Res 
    End Function 

    Friend Function ConnectionFromMatch(ByVal con As PropertyConnection, ByVal ClassGuid As String) As Boolean 
     If con.PaintPluginFrom Is Nothing Then Return False 
     If con.PaintPluginFrom.Plugin Is Nothing Then Return False 
     If con.PaintPluginFrom.Plugin.Guid = ClassGuid Then Return True 
     Return False 
    End Function 
End Class 

これはどのように使用できますか?あなたのコメントに答えるために

Res = LBaseConnections.FindAll(Function(con) ConnectionFromMatch(con, ClassGuid)) 

EDIT:

答えて

2

、2番目のパラメータを渡すために、ラムダ式を使用し

FindAllPredicate(Of T)(あなたのケースでPredicate(Of PropertyConnection)を)取るので、あなたがすることはできません署名には互換性がないため、ConMatchDelegateを渡してください。だから私はPredicate(Of PropertyConnection)を匿名メソッドを使って作成します。これはもっとわかりやすいでしょう:

Dim filter As Predicate(Of PropertyConnection) = Function(con) ConnectionFromMatch(con, ClassGuid) 
Res = LBaseConnections.FindAll(filter) 
+0

ありがとうございました。そしてそれは魅力のように機能します。しかし、なぜ私は理解していない。より多くの説明やlamnda式の代わりにデリゲートで書き直すことができますか、あるいはあなたの行にコメントを書くことができますか? – Nasenbaer

+0

@Nasenbaer、私の編集した回答を参照 –

0

私はC#の男ですが、私はここで概念を共有することができます。

FindAll Listクラスのメソッドは、の述語をとります。 Predicateは単一のパラメーターを取り、ブール値を返す特殊なデリゲートです。したがって、FindAllは内部的にリスト内の各項目の反復を行い、どの項目が述語で定義された条件を満たすかは結果に含まれます。

class Program 
    { 
     static void Main(string[] args) 
     { 
      List<PropertyConnection> lstConn = new List<PropertyConnection>(){ 
                new PropertyConnection() { Id = 10}, 
                new PropertyConnection() { Id = 20 }, 
                new PropertyConnection() { Id = 30 } }; 

      List<PropertyConnection> filtered = lstConn.FindAll(MyDelegate); 
      // so filtered contains just one item with Id = 30 
     } 

     private static bool MyDelegate(PropertyConnection con) // your own delegate 
     { 
      if (con.Id > 20) 
       return true; 
      else 
       return false; 
     } 
    } 

    public class PropertyConnection // sample class 
    { 
     public int Id; 
    } 
+0

こんにちはSantoo。私はまだ同じ問題を抱えています。 MyDelegateに「変数」を提出する方法は? Thomas Levesqueは既に働いているラムダ式の解法で答えました。しかし、そこに私はなぜ完全に理解していない。ご協力いただきありがとうございます。 – Nasenbaer

関連する問題