List<T>.Find(Predicate<T>) Metodo

Definizione

Cerca un elemento che soddisfi le condizioni definite nel predicato specificato e restituisce la prima occorrenza all'interno dell'intero oggetto List<T>.

public:
 T Find(Predicate<T> ^ match);
public T Find (Predicate<T> match);
public T? Find (Predicate<T> match);
member this.Find : Predicate<'T> -> 'T
Public Function Find (match As Predicate(Of T)) As T

Parametri

match
Predicate<T>

Delegato Predicate<T> che definisce le condizioni dell'elemento da cercare.

Restituisce

T

Primo elemento che soddisfa le condizioni definite dal predicato specificato, se trovato; in caso contrario, viene restituito il valore predefinito del tipo T.

Eccezioni

match è null.

Esempio

Nell'esempio seguente viene illustrato il metodo Find su un List<T> contenente un oggetto complesso semplice.

using System;
using System.Collections.Generic;
// Simple business object. A PartId is used to identify a part
// but the part name can change.
public class Part : IEquatable<Part>
{
    public string PartName { get; set; }
    public int PartId { get; set; }

    public override string ToString()
    {
        return "ID: " + PartId + "   Name: " + PartName;
    }
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        Part objAsPart = obj as Part;
        if (objAsPart == null) return false;
        else return Equals(objAsPart);
    }
    public override int GetHashCode()
    {
        return PartId;
    }
    public bool Equals(Part other)
    {
        if (other == null) return false;
        return (this.PartId.Equals(other.PartId));
    }
    // Should also override == and != operators.
}
public class Example
{
    public static void Main()
    {
        // Create a list of parts.
        List<Part> parts = new List<Part>();

        // Add parts to the list.
        parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
        parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
        parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
        parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
        parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
        parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ;

        // Write out the parts in the list. This will call the overridden ToString method
        // in the Part class.
        Console.WriteLine();
        foreach (Part aPart in parts)
        {
            Console.WriteLine(aPart);
        }

        // Check the list for part #1734. This calls the IEquatable.Equals method
        // of the Part class, which checks the PartId for equality.
        Console.WriteLine("\nContains: Part with Id=1734: {0}",
            parts.Contains(new Part { PartId = 1734, PartName = "" }));

        // Find items where name contains "seat".
        Console.WriteLine("\nFind: Part where name contains \"seat\": {0}",
            parts.Find(x => x.PartName.Contains("seat")));

        // Check if an item with Id 1444 exists.
        Console.WriteLine("\nExists: Part with Id=1444: {0}",
            parts.Exists(x => x.PartId == 1444));

        /*This code example produces the following output:

        ID: 1234   Name: crank arm
        ID: 1334   Name: chain ring
        ID: 1434   Name: regular seat
        ID: 1444   Name: banana seat
        ID: 1534   Name: cassette
        ID: 1634   Name: shift lever

        Contains: Part with Id=1734: False

        Find: Part where name contains "seat": ID: 1434   Name: regular seat

        Exists: Part with Id=1444: True
         */
    }
}
Imports System.Collections.Generic

' Simple business object. A PartId is used to identify a part 
' but the part name can change. 
Public Class Part
    Implements IEquatable(Of Part)
    Public Property PartName() As String
        Get
            Return m_PartName
        End Get
        Set(value As String)
            m_PartName = Value
        End Set
    End Property
    Private m_PartName As String
    Public Property PartId() As Integer
        Get
            Return m_PartId
        End Get
        Set(value As Integer)
            m_PartId = Value
        End Set
    End Property
    Private m_PartId As Integer

    Public Overrides Function ToString() As String
        Return Convert.ToString("ID: " & PartId & "   Name: ") & PartName
    End Function
    Public Overrides Function Equals(obj As Object) As Boolean
        If obj Is Nothing Then
            Return False
        End If
        Dim objAsPart As Part = TryCast(obj, Part)
        If objAsPart Is Nothing Then
            Return False
        Else
            Return Equals(objAsPart)
        End If
    End Function
    Public Overrides Function GetHashCode() As Integer
        Return PartId
    End Function
    Public Overloads Function Equals(other As Part) As Boolean _
        Implements IEquatable(Of Part).Equals
        If other Is Nothing Then
            Return False
        End If
        Return (Me.PartId.Equals(other.PartId))
    End Function
    ' Should also override == and != operators.
End Class
Public Class Example
    Public Shared Sub Main()
        ' Create a list of parts.
        Dim parts As New List(Of Part)()

        ' Add parts to the list.
        parts.Add(New Part() With { _
             .PartName = "crank arm", _
             .PartId = 1234 _
        })
        parts.Add(New Part() With { _
             .PartName = "chain ring", _
             .PartId = 1334 _
        })
        parts.Add(New Part() With { _
             .PartName = "regular seat", _
             .PartId = 1434 _
        })
        parts.Add(New Part() With { _
             .PartName = "banana seat", _
             .PartId = 1444 _
        })
        parts.Add(New Part() With { _
             .PartName = "cassette", _
             .PartId = 1534 _
        })
        parts.Add(New Part() With { _
             .PartName = "shift lever", _
             .PartId = 1634 _
        })



        ' Write out the parts in the list. This will call the overridden ToString method
        ' in the Part class.
        Console.WriteLine()
        For Each aPart As Part In parts
            Console.WriteLine(aPart)
        Next


        ' Check the list for part #1734. This calls the IEquatable.Equals method
        ' of the Part class, which checks the PartId for equality.
        Console.WriteLine(vbLf & "Contains: Part with Id=1734: {0}",
                          parts.Contains(New Part() With { _
             .PartId = 1734, _
             .PartName = "" _
        }))

        ' Find items where name contains "seat".
        Console.WriteLine(vbLf & "Find: Part where name contains ""seat"": {0}",
                          parts.Find(Function(x) x.PartName.Contains("seat")))

        ' Check if an item with Id 1444 exists.
        Console.WriteLine(vbLf & "Exists: Part with Id=1444: {0}",
                          parts.Exists(Function(x) x.PartId = 1444))

        'This code example produces the following output:
        '        
        '        ID: 1234   Name: crank arm
        '        ID: 1334   Name: chain ring
        '        ID: 1434   Name: regular seat
        '        ID: 1444   Name: banana seat
        '        ID: 1534   Name: cassette
        '        ID: 1634   Name: shift lever
        '
        '        Contains: Part with Id=1734: False
        '
        '        Find: Part where name contains "seat": ID: 1434   Name: regular seat
        '
        '        Exists: Part with Id=1444: True 
        '         

    End Sub
End Class

Nell'esempio seguente vengono illustrati i metodi di ricerca per la List<T> classe. L'esempio List<T> della classe contiene book oggetti, di classe Book, usando i dati del file XML di esempio: libri (LINQ to XML). Il FillList metodo nell'esempio usa LINQ to XML per analizzare i valori dal codice XML ai valori delle proprietà degli book oggetti.

Nella tabella seguente vengono descritti gli esempi forniti per i metodi di ricerca.

Metodo Esempio
Find(Predicate<T>) Trova un libro in base a un ID usando il IDToFind delegato predicato.

L'esempio C# usa un delegato anonimo.
FindAll(Predicate<T>) Trovare tutti i libri la cui Genre proprietà è "Computer" usando il FindComputer delegato predicato.
FindLast(Predicate<T>) Trova l'ultimo libro nella raccolta con data di pubblicazione prima del 2001 usando il PubBefore2001 delegato predicato.

L'esempio C# usa un delegato anonimo.
FindIndex(Predicate<T>) Trova l'indice del primo libro computer usando il FindComputer delegato predicato.
FindLastIndex(Predicate<T>) Trova l'indice dell'ultimo libro computer usando il FindComputer delegato predicato.
FindIndex(Int32, Int32, Predicate<T>) Trova l'indice del primo libro computer nella seconda metà della raccolta, usando il FindComputer delegato predicato.
FindLastIndex(Int32, Int32, Predicate<T>) Trova l'indice dell'ultimo libro computer nella seconda metà della raccolta, usando il FindComputer delegato predicato.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Find
{
    class Program
    {
        private static string IDtoFind = "bk109";

        private static List<Book> Books = new List<Book>();
        public static void Main(string[] args)
        {
            FillList();

            // Find a book by its ID.
            Book result = Books.Find(
            delegate(Book bk)
            {
                return bk.ID == IDtoFind;
            }
            );
            if (result != null)
            {
                DisplayResult(result, "Find by ID: " + IDtoFind);
            }
            else
            {
                Console.WriteLine("\nNot found: {0}", IDtoFind);
            }

            // Find last book in collection published before 2001.
            result = Books.FindLast(
            delegate(Book bk)
            {
                DateTime year2001 = new DateTime(2001,01,01);
                return bk.Publish_date < year2001;
            });
            if (result != null)
            {
                DisplayResult(result, "Last book in collection published before 2001:");
            }
            else
            {
                Console.WriteLine("\nNot found: {0}", IDtoFind);
            }

            // Find all computer books.
            List<Book> results = Books.FindAll(FindComputer);
            if (results.Count != 0)
            {
                DisplayResults(results, "All computer:");
            }
            else
            {
                Console.WriteLine("\nNo books found.");
            }

            // Find all books under $10.00.
            results = Books.FindAll(
            delegate(Book bk)
            {
                return bk.Price < 10.00;
            }
            );
            if (results.Count != 0)
            {
                DisplayResults(results, "Books under $10:");
            }
            else
            {
                Console.WriteLine("\nNo books found.");
            }

            // Find index values.
            Console.WriteLine();
            int ndx = Books.FindIndex(FindComputer);
            Console.WriteLine("Index of first computer book: {0}", ndx);
            ndx = Books.FindLastIndex(FindComputer);
            Console.WriteLine("Index of last computer book: {0}", ndx);

            int mid = Books.Count / 2;
            ndx = Books.FindIndex(mid, mid, FindComputer);
            Console.WriteLine("Index of first computer book in the second half of the collection: {0}", ndx);

            ndx = Books.FindLastIndex(Books.Count - 1, mid, FindComputer);
            Console.WriteLine("Index of last computer book in the second half of the collection: {0}", ndx);
        }

        // Populates the list with sample data.
        private static void FillList()
        {

            // Create XML elements from a source file.
            XElement xTree = XElement.Load(@"c:\temp\books.xml");

            // Create an enumerable collection of the elements.
            IEnumerable<XElement> elements = xTree.Elements();

            // Evaluate each element and set set values in the book object.
            foreach (XElement el in elements)
            {
                Book book = new Book();
                book.ID = el.Attribute("id").Value;
                IEnumerable<XElement> props = el.Elements();
                foreach (XElement p in props)
                {

                    if (p.Name.ToString().ToLower() == "author")
                    {
                        book.Author = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "title")
                    {
                        book.Title = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "genre")
                    {
                        book.Genre = p.Value;
                    }
                    else if (p.Name.ToString().ToLower() == "price")
                    {
                        book.Price = Convert.ToDouble(p.Value);
                    }
                    else if (p.Name.ToString().ToLower() == "publish_date")
                    {
                        book.Publish_date = Convert.ToDateTime(p.Value);
                    }
                    else if (p.Name.ToString().ToLower() == "description")
                    {
                        book.Description = p.Value;
                    }
                }

                Books.Add(book);
            }

            DisplayResults(Books, "All books:");
        }

        // Explicit predicate delegate.
        private static bool FindComputer(Book bk)
        {

            if (bk.Genre == "Computer")
            {
                return true;
            }
        else
            {
                return false;
            }
        }

        private static void DisplayResult(Book result, string title)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            Console.WriteLine("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", result.ID,
                result.Author, result.Title, result.Genre, result.Price,
                result.Publish_date.ToShortDateString());
            Console.WriteLine();
        }

        private static void DisplayResults(List<Book> results, string title)
        {
            Console.WriteLine();
            Console.WriteLine(title);
            foreach (Book b in results)
            {

                Console.Write("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", b.ID,
                    b.Author, b.Title, b.Genre, b.Price,
                    b.Publish_date.ToShortDateString());
            }
            Console.WriteLine();
        }
    }

    public class Book
    {
        public string ID { get; set; }
        public string Author { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
        public double Price { get; set; }
        public DateTime Publish_date { get; set; }
        public string Description { get; set; }
    }
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Module Module1

    Private IDToFind As String = "bk109"

    Public Books As New List(Of Book)


    Sub Main()

        FillList()

        ' Find a book by its ID.
        Dim result As Book = Books.Find(AddressOf FindID)
        If result IsNot Nothing Then
            DisplayResult(result, "Find by ID: " & IDToFind)
        Else

            Console.WriteLine(vbCrLf & "Not found: " & IDToFind)
        End If
        Console.WriteLine()

        ' Find last book in collection that has a publish date before 2001.
        result = Books.FindLast(AddressOf PubBefore2001)
        If result IsNot Nothing Then
            DisplayResult(result, "Last book in collection published before 2001:")
        Else
            Console.WriteLine(vbCrLf & "Not found: " & IDToFind)
        End If
        Console.WriteLine()

        ' Find all computer books.
        Dim results As List(Of Book) = Books.FindAll(AddressOf FindComputer)
        If results.Count <> 0 Then
            DisplayResults(results, "All computer books:")
        Else
            Console.WriteLine(vbCrLf & "No books found.")
        End If
        Console.WriteLine()


        ' Find all books under $10.00.
        results = Books.FindAll(AddressOf FindUnderTen)
        If results.Count <> 0 Then
            DisplayResults(results, "Books under $10:")
        Else
            Console.WriteLine(vbCrLf & "No books found.")
        End If
        Console.WriteLine()

        ' Find index values.
        Console.WriteLine()
        Dim ndx As Integer = Books.FindIndex(AddressOf FindComputer)
        Console.WriteLine("Index of first computer book: " & ndx)
        ndx = Books.FindLastIndex(AddressOf FindComputer)
        Console.WriteLine("Index of last computer book: " & ndx)

        Dim mid As Integer = Books.Count / 2
        ndx = Books.FindIndex(mid, mid, AddressOf FindComputer)
        Console.WriteLine("Index of first computer book in the second half of the collection: " & ndx)



        ndx = Books.FindLastIndex(Books.Count - 1, mid, AddressOf FindComputer)
        Console.WriteLine("Index of last computer book in the second half of the collection: " & ndx)


    End Sub



    Private Sub FillList()

        ' Create XML elements from a source file.
        Dim xTree As XElement = XElement.Load("c:\temp\books.xml")

        ' Create an enumerable collection of the elements.
        Dim elements As IEnumerable(Of XElement) = xTree.Elements

        ' Evaluate each element and set values in the book object.
        For Each el As XElement In elements
            Dim Book As New Book()
            Book.ID = el.Attribute("id").Value
            Dim props As IEnumerable(Of XElement) = el.Elements
            For Each p As XElement In props
                If p.Name.ToString.ToLower = "author" Then
                    Book.Author = p.Value
                End If
                If p.Name.ToString.ToLower = "title" Then
                    Book.Title = p.Value
                End If
                If p.Name.ToString.ToLower = "genre" Then
                    Book.Genre = p.Value
                End If
                If p.Name.ToString.ToLower = "price" Then
                    Book.Price = Convert.ToDouble(p.Value)
                End If
                If p.Name.ToString.ToLower = "publish_date" Then
                    Book.Publish_date = Convert.ToDateTime(p.Value)
                End If
                If p.Name.ToString.ToLower = "description" Then
                    Book.Description = p.Value
                End If
            Next
            Books.Add(Book)
        Next

        DisplayResults(Books, "All books:")
        Console.WriteLine()

    End Sub

    ' Predicate delegates for
    ' Find and FindAll methods.
    Private Function FindID(ByVal bk As Book) As Boolean
        If bk.ID = IDToFind Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function FindComputer(ByVal bk As Book) As Boolean
        If bk.Genre = "Computer" Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function FindUnderTen(ByVal bk As Book) As Boolean
        Dim tendollars As Double = 10.0
        If bk.Price < tendollars Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function PubBefore2001(ByVal bk As Book) As Boolean
        Dim year2001 As DateTime = New DateTime(2001, 1, 1)
        Return bk.Publish_date < year2001
    End Function
    Private Sub DisplayResult(ByVal result As Book, ByVal title As String)
        Console.WriteLine()
        Console.WriteLine(title)
        Console.WriteLine(vbLf & result.ID & vbTab & result.Author & _
                          vbTab & result.Title & vbTab & result.Genre & _
                          vbTab & result.Publish_date & vbTab & result.Price)
        Console.WriteLine()
    End Sub
    Private Sub DisplayResults(ByVal results As List(Of Book), ByVal title As String)
        Console.WriteLine()
        Console.WriteLine(title)
        For Each b As Book In results
            Console.Write(vbLf & b.ID & vbTab & b.Author & _
                              vbTab & b.Title & vbTab & b.Genre & _
                              vbTab & b.Publish_date & vbTab & b.Price)
        Next
        Console.WriteLine()
    End Sub

    Public Class Book
        Public ID As String
        Public Author As String
        Public Title As String
        Public Genre As String
        Public Price As Double
        Public Publish_date As DateTime
        Public Description As String
    End Class


End Module

Commenti

L'oggetto Predicate<T> è un delegato a un metodo che restituisce true se l'oggetto passato corrisponde alle condizioni definite nel delegato. Gli elementi dell'oggetto corrente List<T> vengono passati singolarmente al Predicate<T> delegato, spostandosi avanti in List<T>, a partire dal primo elemento e terminando con l'ultimo elemento. L'elaborazione viene arrestata quando viene trovata una corrispondenza.

Importante

Quando si esegue la ricerca di un elenco contenente tipi di valore, assicurarsi che il valore predefinito per il tipo non soddisfi il predicato di ricerca. In caso contrario, non è possibile distinguere tra un valore predefinito che indica che non è stata trovata alcuna corrispondenza e un elemento di elenco che accade per avere il valore predefinito per il tipo. Se il valore predefinito soddisfa il predicato di ricerca, usare invece il FindIndex metodo .

Questo metodo esegue una ricerca lineare; pertanto, questo metodo è un'operazione O(n), dove n è Count.

Si applica a

Vedi anche