EntityCollection<TEntity>.Contains(TEntity) 메서드

정의

특정 개체가 컬렉션에 있는지 여부를 확인합니다.

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

매개 변수

entity
TEntity

EntityCollection<TEntity>에서 찾을 개체입니다.

반환

EntityCollection<TEntity>에 개체가 있으면 true이고, 그렇지 않으면 false입니다.

구현

예제

이 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제에서 코드를 실행하려면 프로젝트에 AdventureWorks 판매 모델을 이미 추가하고 Entity Framework를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: 수동으로 Entity Framework 프로젝트 구성방법: 모델 및 매핑 파일 수동 정의의 절차를 완료합니다.

이 예에서는 다음을 수행합니다.

  1. 두 개의 새 SalesOrderHeader 엔터티를 만들고 엔터티에 Contact 추가합니다.

  2. 연락처 엔터티와 연결된 에서 RelationshipManager 모든 관련 끝을 가져옵니다.

  3. IRelatedEnd컬렉션을 반복합니다.

  4. 각 관련 끝의 EntityCollection<TEntity> 를 가져옵니다.

  5. 메서드를 Remove 사용하여 컬렉션에서 엔터티 중 하나를 제거합니다.

  6. 메서드를 Contains 호출하여 개체가 컬렉션에서 제거되었는지 여부를 확인합니다.

  7. 메서드를 Add 사용하여 엔터티를 다시 추가합니다.

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

설명

메서드를 Object.Equals 사용하여 지정된 개체를 컬렉션에 이미 있는 개체와 비교합니다.

적용 대상