EntityCollection<TEntity>.Contains(TEntity) 方法
定义
确定特定对象是否存在于集合中。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
参数
- entity
- TEntity
要在 EntityCollection<TEntity> 中定位的对象。The object to locate in the EntityCollection<TEntity>.
返回
如果在 EntityCollection<TEntity> 中找到对象,则为 true;否则为 false。true if the object is found in the EntityCollection<TEntity>; otherwise, false.
实现
示例
此示例基于 艾德公司销售模型。This example is based on the Adventure Works Sales Model. 若要运行此示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用实体框架。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:
创建两个新的
SalesOrderHeader实体并添加到Contact实体。Creates two newSalesOrderHeaderentities and adds them to theContactentity.从与 Contact 实体关联的 RelationshipManager 中获取所有相关端。Gets all related ends from the RelationshipManager that is associated with the Contact entity.
循环访问 IRelatedEnd 集合。Iterates through the collection of IRelatedEnds.
获取每个相关端的 EntityCollection<TEntity>。Gets the EntityCollection<TEntity> for each related end.
使用 Remove 方法从集合中移除其中一个实体。Uses the Remove method to remove one of the entities from the collection.
调用 Contains 方法确定该对象是否已从集合中移除。Calls the Contains method to determine whether the object was removed from the collection.
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
注解
使用 Object.Equals 方法来比较指定对象和集合中已经存在的对象。Uses the Object.Equals method to compare the specified object with the objects already in the collection.