EntityCollection<TEntity>.Contains(TEntity) Metodo

Definizione

Determina se un oggetto specifico esiste nell'insieme.

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

Parametri

entity
TEntity

Oggetto da individuare nell'oggetto EntityCollection<TEntity>.

Valori restituiti

true se l'oggetto viene trovato in ; in EntityCollection<TEntity>caso contrario, false.

Implementazioni

Esempio

Questo esempio è basato sul modello Adventure Works Sales. Per eseguire il codice in questo esempio, è necessario aver già aggiunto il modello AdventureWorks Sales al progetto e configurato il progetto per l'uso di Entity Framework. A tale scopo, completare le procedure in Come: Configurare manualmente entity Framework Project e Come: Definire manualmente i file di modello e mapping.

In questo esempio vengono eseguite le operazioni seguenti:

  1. Crea due nuove SalesOrderHeader entità e le aggiunge all'entità Contact .

  2. Ottiene tutte le estremità correlate dall'oggetto RelationshipManager associato all'entità Contact.

  3. Scorre la raccolta di IRelatedEndoggetti .

  4. Ottiene l'oggetto EntityCollection<TEntity> per ogni fine correlata.

  5. Usa il Remove metodo per rimuovere una delle entità dalla raccolta.

  6. Chiama il Contains metodo per determinare se l'oggetto è stato rimosso dall'insieme.

  7. Usa il Add metodo per aggiungere nuovamente l'entità.

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)
            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);
    }
}

Commenti

Utilizza il Object.Equals metodo per confrontare l'oggetto specificato con gli oggetti già presenti nell'insieme.

Si applica a