Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo EntityCollection<TEntity>.Contains (TEntity)

 

Determina se nella raccolta è presente un oggetto specifico.

Spazio dei nomi:   System.Data.Objects.DataClasses
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public bool Contains(
	TEntity entity
)

Parametri

entity
Type: TEntity

Oggetto da individuare nella raccolta EntityCollection<TEntity>.

Valore restituito

Type: System.Boolean

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

Uses the T:System.Object.Equals method to compare the specified object with the objects already in the collection.

This example is based on the . 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. 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.

This example does the following:

  1. Creates two new SalesOrderHeader entities and adds them to the Contact entity.

  2. Gets all related ends from the T:System.Data.Objects.DataClasses.RelationshipManager that is associated with the Contact entity.

  3. Iterates through the collection of T:System.Data.Objects.DataClasses.IRelatedEnds.

  4. Gets the T:System.Data.Objects.DataClasses.EntityCollection`1 for each related end.

  5. Uses the M:System.Data.Objects.DataClasses.EntityCollection`1.Remove(`0) method to remove one of the entities from the collection.

  6. Calls the M:System.Data.Objects.DataClasses.EntityCollection`1.Contains(`0) method to determine whether the object was removed from the collection.

  7. Uses the M:System.Data.Objects.DataClasses.EntityCollection`1.Add(`0) 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);
    }
}

.NET Framework
Disponibile da 3.5
Torna all'inizio
Mostra: