EntityCollection<TEntity>.Contains(TEntity) Methode

Definition

Ermittelt, ob die Auflistung ein bestimmtes Objekt enthält.Determines whether a specific object exists in the collection.

public:
 virtual bool Contains(TEntity entity);
public bool Contains (TEntity entity);
abstract member Contains : 'Entity -> bool
override this.Contains : 'Entity -> bool
Public Function Contains (entity As TEntity) As Boolean

Parameter

entity
TEntity

Das in EntityCollection<TEntity> zu suchende Objekt.The object to locate in the EntityCollection<TEntity>.

Gibt zurück

true, wenn das Objekt in EntityCollection<TEntity> gefunden wird, andernfalls false.true if the object is found in the EntityCollection<TEntity>; otherwise, false.

Implementiert

Beispiele

Dieses Beispiel beruht auf dem .This example is based on the . Zum Ausführen des Codes in diesem Beispiel müssen Sie dem Projekt bereits das AdventureWorks Sales-Modell hinzugefügt und das Projekt zur Verwendung des Entity Framework konfiguriert haben.To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. Führen Sie hierzu die Verfahren unter Gewusst wie: Manuelles Konfigurieren eines Entity Framework Projekts und Gewusst wie: Manuelles Definieren der Modell-und Mapping-Dateienaus.To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define the Model and Mapping Files.

In diesem Beispiel werden die folgenden Aufgaben ausgeführt:This example does the following:

  1. Zwei neue SalesOrderHeader-Entitäten werden erstellt und der Contact-Entität hinzugefügt.Creates two new SalesOrderHeader entities and adds them to the Contact entity.

  2. Ruft alle verknüpften Enden vom RelationshipManager ab, der der "Contact"-Entität zugeordnet ist.Gets all related ends from the RelationshipManager that is associated with the Contact entity.

  3. Durchläuft die Auflistung von IRelatedEnd.Iterates through the collection of IRelatedEnds.

  4. Ruft die EntityCollection<TEntity> für jedes verknüpfte Ende ab.Gets the EntityCollection<TEntity> for each related end.

  5. Verwendet die Remove-Methode, um eine der Entitäten aus der Auflistung zu entfernen.Uses the Remove method to remove one of the entities from the collection.

  6. Ruft die Contains-Methode auf, um zu ermitteln, ob das Objekt aus der Auflistung entfernt wurde.Calls the Contains method to determine whether the object was removed from the collection.

  7. Verwendet die Add-Methode, um die Entität erneut hinzuzufügen.Uses the Add method to add the entity back.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    Contact contact = new Contact();

    // Create a new SalesOrderHeader.
    SalesOrderHeader newSalesOrder1 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.
    contact.SalesOrderHeaders.Add(newSalesOrder1);

    // Create another SalesOrderHeader.
    SalesOrderHeader newSalesOrder2 = new SalesOrderHeader();
    // Add SalesOrderHeader to the Contact.
    contact.SalesOrderHeaders.Add(newSalesOrder2);

    // Get all related ends
    IEnumerable<IRelatedEnd> relEnds =
        ((IEntityWithRelationships)contact)
        .RelationshipManager.GetAllRelatedEnds();

    foreach (IRelatedEnd relEnd in relEnds)
    {
        // Get Entity Collection from related end
        EntityCollection<SalesOrderHeader> entityCollection =
            (EntityCollection<SalesOrderHeader>)relEnd;

        Console.WriteLine("EntityCollection count: {0}",
            entityCollection.Count);
        // Remove the first entity object.
        entityCollection.Remove(newSalesOrder1);

        bool contains = entityCollection.Contains(newSalesOrder1);

        // Write the number of items after one entity has been removed
        Console.WriteLine("EntityCollection count after one entity has been removed: {0}",
            entityCollection.Count);

        if (contains == false)
            Console.WriteLine("The removed entity is not in in the collection any more.");

        //Use IRelatedEnd to add the entity back.
        relEnd.Add(newSalesOrder1);
        Console.WriteLine("EntityCollection count after an entity has been added again: {0}",
            entityCollection.Count);
    }
}
Using context As New AdventureWorksEntities()
    Dim contact As New Contact()

    ' Create a new SalesOrderHeader. 
    Dim newSalesOrder1 As New SalesOrderHeader()
    ' Add SalesOrderHeader to the Contact. 
    contact.SalesOrderHeaders.Add(newSalesOrder1)

    ' Create another SalesOrderHeader. 
    Dim newSalesOrder2 As New SalesOrderHeader()
    ' Add SalesOrderHeader to the Contact. 
    contact.SalesOrderHeaders.Add(newSalesOrder2)

    ' Get all related ends 
    Dim relEnds As IEnumerable(Of IRelatedEnd) = DirectCast(contact, IEntityWithRelationships).RelationshipManager.GetAllRelatedEnds()

    For Each relEnd As IRelatedEnd In relEnds
        ' Get Entity Collection from related end 
        Dim entityCollection As EntityCollection(Of SalesOrderHeader) = DirectCast(relEnd, EntityCollection(Of SalesOrderHeader))

        Console.WriteLine("EntityCollection count: {0}", entityCollection.Count)
        ' Remove the first entity object. 
        entityCollection.Remove(newSalesOrder1)

        Dim contains As Boolean = entityCollection.Contains(newSalesOrder1)

        ' Write the number of items after one entity has been removed 
        Console.WriteLine("EntityCollection count after one entity has been removed: {0}", entityCollection.Count)

        If contains = False Then
            Console.WriteLine("The removed entity is not in in the collection any more.")
        End If

        'Use IRelatedEnd to add the entity back. 
        relEnd.Add(newSalesOrder1)
        Console.WriteLine("EntityCollection count after an entity has been added again: {0}", entityCollection.Count)
    Next
End Using

Hinweise

Verwendet die Object.Equals-Methode, um das angegebene Objekt mit den bereits in der Auflistung enthaltenen Objekten zu vergleichen.Uses the Object.Equals method to compare the specified object with the objects already in the collection.

Gilt für: