EntityCollection<TEntity>.Contains(TEntity) Metoda

Definicja

Określa, czy określony obiekt istnieje w kolekcji.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

Parametry

entity
TEntity

Obiekt, który ma zostać zlokalizowany w EntityCollection<TEntity> .The object to locate in the EntityCollection<TEntity>.

Zwraca

Boolean

true Jeśli obiekt znajduje się w; w EntityCollection<TEntity> przeciwnym razie, false .true if the object is found in the EntityCollection<TEntity>; otherwise, false.

Implementuje

Przykłady

Ten przykład jest oparty na modelu sprzedaży Adventure Works.This example is based on the Adventure Works Sales Model. Aby uruchomić kod w tym przykładzie, musisz już dodać model sprzedaży AdventureWorks do projektu i skonfigurować projekt do korzystania z Entity Framework.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. Aby to zrobić, wykonaj procedury opisane w temacie jak: ręcznie skonfigurować projekt Entity Framework i instrukcje: ręcznie zdefiniować model i pliki mapowania.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.

Ten przykład wykonuje następujące czynności:This example does the following:

  1. Tworzy dwie nowe SalesOrderHeader jednostki i dodaje je do Contact jednostki.Creates two new SalesOrderHeader entities and adds them to the Contact entity.

  2. Pobiera wszystkie powiązane zakończenia z usługi RelationshipManager , która jest skojarzona z jednostką Contact.Gets all related ends from the RelationshipManager that is associated with the Contact entity.

  3. Wykonuje iterację kolekcji IRelatedEnd s.Iterates through the collection of IRelatedEnds.

  4. Pobiera EntityCollection<TEntity> dla każdego powiązanego końca.Gets the EntityCollection<TEntity> for each related end.

  5. Używa Remove metody do usunięcia jednej z jednostek z kolekcji.Uses the Remove method to remove one of the entities from the collection.

  6. Wywołuje Contains metodę, aby określić, czy obiekt został usunięty z kolekcji.Calls the Contains method to determine whether the object was removed from the collection.

  7. Używa Add metody, aby dodać jednostkę z powrotem.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

Uwagi

Używa Object.Equals metody do porównania określonego obiektu z obiektami znajdującymi się już w kolekcji.Uses the Object.Equals method to compare the specified object with the objects already in the collection.

Dotyczy