EntityCollection<TEntity>.Contains(TEntity) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
Parameters
- entity
- TEntity
The object to locate in the EntityCollection<TEntity>.
Returns
true
if the object is found in the EntityCollection<TEntity>; otherwise, false
.
Implements
Examples
This example is based on the Adventure Works Sales Model. 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:
Creates two new
SalesOrderHeader
entities and adds them to theContact
entity.Gets all related ends from the RelationshipManager that is associated with the Contact entity.
Iterates through the collection of IRelatedEnds.
Gets the EntityCollection<TEntity> for each related end.
Uses the Remove method to remove one of the entities from the collection.
Calls the Contains method to determine whether the object was removed from the collection.
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)
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);
}
}
Remarks
Uses the Object.Equals method to compare the specified object with the objects already in the collection.