EntityCollection<TEntity> 类

定义

表示关系“多”端的对象集合。Represents a collection of objects on the "many" end of a relationship.

generic <typename TEntity>
 where TEntity : classpublic ref class EntityCollection sealed : System::Data::Objects::DataClasses::RelatedEnd, System::Collections::Generic::ICollection<TEntity>, System::Collections::Generic::IEnumerable<TEntity>, System::ComponentModel::IListSource
[System.Serializable]
public sealed class EntityCollection<TEntity> : System.Data.Objects.DataClasses.RelatedEnd, System.Collections.Generic.ICollection<TEntity>, System.Collections.Generic.IEnumerable<TEntity>, System.ComponentModel.IListSource where TEntity : class
type EntityCollection<'Entity (requires 'Entity : null)> = class
    inherit RelatedEnd
    interface ICollection<'Entity (requires 'Entity : null)>
    interface seq<'Entity (requires 'Entity : null)>
    interface IEnumerable
    interface IListSource
Public NotInheritable Class EntityCollection(Of TEntity)
Inherits RelatedEnd
Implements ICollection(Of TEntity), IEnumerable(Of TEntity), IListSource

类型参数

TEntity

集合的实体类型。The entity type of the collection.

继承
EntityCollection<TEntity>
属性
实现

示例

此示例基于。This example is based on the . 若要运行此示例中的代码,必须已将 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:

  1. 创建两个新的 SalesOrderHeader 实体并添加到 Contact 实体。Creates two new SalesOrderHeader entities and adds them to the Contact entity.

  2. 从与 RelationshipManager 实体关联的 Contact 中获取所有相关端。Gets all related ends from the RelationshipManager that is associated with the Contact entity.

  3. 循环访问 IRelatedEnd 集合。Iterates through the collection of IRelatedEnds.

  4. 获取每个相关端的 EntityCollection<TEntity>Gets the EntityCollection<TEntity> for each related end.

  5. 使用 Remove 方法从集合中移除其中一个实体。Uses the Remove method to remove one of the entities from the collection.

  6. 调用 Contains 方法确定该对象是否已从集合中移除。Calls the Contains method to determine whether the object was removed from the collection.

  7. 使用 Add 方法添加回实体。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 == 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

注解

EntityCollection<TEntity> 是特定实体类型的对象集合,该实体类型表示一对多或多对多关系的“多”端。An EntityCollection<TEntity> is a collection of objects of a particular entity type that represents the "many" end of a one-to-many or many-to-many relationship.

由导航属性返回一个 EntityCollection<TEntity>An EntityCollection<TEntity> is returned by a navigation property. 使用 Load 方法可以将相关对象加载到 EntityCollection<TEntity> 中。Use the Load method to load related objects into an EntityCollection<TEntity>. 要存储特定实体类型的对象的非相关集合,例如 ObjectQuery<T> 的结果,请使用 List<T> 类的实例。To store an unrelated collection of objects of a specific entity type, such as the result of an ObjectQuery<T>, use an instance of the List<T> class.

EntityCollection<TEntity> 可能有对应的 EntityReference<TEntity>An EntityCollection<TEntity> might have a corresponding EntityReference<TEntity>. EntityCollection<TEntity>EntityReference<TEntity> 模型位于同一关系的相反端时,该关系的完整性维持在对象级别。When an EntityCollection<TEntity> and an EntityReference<TEntity> model opposite ends of the same relationship, the integrity of the relationship is maintained at the object level. 两个类自动同步。The two classes are synchronized automatically.

此类不能被继承。This class cannot be inherited.

构造函数

EntityCollection<TEntity>()

初始化 EntityCollection<TEntity> 类的新实例。Initializes a new instance of the EntityCollection<TEntity> class.

属性

Count

获取集合中包含的对象数。Gets the number of objects that are contained in the collection.

IsLoaded

获取一个值,该值指示是否已加载所有相关对象。Gets a value that indicates whether all related objects have been loaded.

(继承自 RelatedEnd)
IsReadOnly

获取一个值,该值指示 EntityCollection<TEntity> 是否为只读。Gets a value that indicates whether the EntityCollection<TEntity> is read-only.

RelationshipName

获取此相关端所参与的关系的名称。Gets the name of the relationship in which this related end participates.

(继承自 RelatedEnd)
RelationshipSet

获取对相关端元数据的引用。Gets a reference to the metadata for the related end.

(继承自 RelatedEnd)
SourceRoleName

获取关系源端处的角色名称。Gets the role name at the source end of the relationship.

(继承自 RelatedEnd)
TargetRoleName

获取关系目标端处的角色名称。Gets the role name at the target end of the relationship.

(继承自 RelatedEnd)

方法

Add(TEntity)

将对象添加到集合。Adds an object to the collection.

Attach(IEnumerable<TEntity>)

定义对象和相关对象集合在对象上下文中的关系。Defines relationships between an object and a collection of related objects in an object context.

Attach(TEntity)

定义两个附加对象在对象上下文中的关系。Defines a relationship between two attached objects in an object context.

Attach<TEntity>(IEnumerable<TEntity>, Boolean)

定义两个附加对象之间的关系。Defines a relationship between two attached objects.

(继承自 RelatedEnd)
Clear()

从集合中移除所有实体。Removes all entities from the collection.

Contains(TEntity)

确定特定对象是否存在于集合中。Determines whether a specific object exists in the collection.

CopyTo(TEntity[], Int32)

将集合中的所有内容复制到数组,从目标数组的指定索引处开始。Copies all the contents of the collection to an array, starting at the specified index of the target array.

CreateSourceQuery()

返回一个对象查询,执行该查询时返回的对象集合与当前集合包含的对象相同。Returns an object query that, when it is executed, returns the same set of objects that exists in the current collection.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回一个枚举器,用于循环访问集合中的对象。Returns an enumerator that is used to iterate through the objects in the collection.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Load()

在派生类中重写时,使用默认合并选项将一个或多个相关对象加载到相关端中。When overridden in a derived class, loads the related object or objects into the related end with the default merge option.

(继承自 RelatedEnd)
Load(MergeOption)

使用指定的合并选项将相关对象加载到集合中。Loads related objects into the collection, using the specified merge option.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean)

将相关实体合并到本地 EntityCollection<TEntity> 中。Merges related entities into the local EntityCollection<TEntity>.

(继承自 RelatedEnd)
OnCollectionDeserialized(StreamingContext)

在内部使用,用于反序列化实体对象。Used internally to deserialize entity objects.

OnDeserialized(StreamingContext)

在内部使用,用于反序列化实体对象与 RelationshipManager 实例。Used internally to deserialize entity objects along with the RelationshipManager instances.

(继承自 RelatedEnd)
OnSerializing(StreamingContext)

在内部使用,用于序列化实体对象。Used internally to serialize entity objects.

Remove(TEntity)

从集合中移除对象,将关系标记为待删除。Removes an object from the collection and marks the relationship for deletion.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean)

确定对象可以附加到本地集合还是引用。Determines whether an object can be attached to the local collection or reference.

(继承自 RelatedEnd)
ValidateLoad<TEntity>(MergeOption, String)

确保一个或多个相关对象可以成功加载到本地集合或引用中。Ensures that the related object or objects can be successfully loaded into the local collection or reference.

(继承自 RelatedEnd)
ValidateOwnerForAttach()

确保相关端所属的对象支持附加操作。Ensures that the object to which the related end belongs supports an attach operation.

(继承自 RelatedEnd)

事件

AssociationChanged

对相关端进行更改时发生。Occurs when a change is made to a related end.

(继承自 RelatedEnd)

显式界面实现

IEnumerable.GetEnumerator()

返回一个枚举器,用于循环访问 EntityCollection<TEntity> 缓存的值集。Returns an enumerator that is used to iterate through the set of values cached by EntityCollection<TEntity>.

IListSource.ContainsListCollection

获取一个值,该值指示相关实体集合是否包含集合对象。Gets a value that indicates whether the related entity collection consists of collection objects.

IListSource.GetList()

IList 形式返回一个集合,用于数据绑定。Returns the collection as an IList used for data binding.

IRelatedEnd.Add(IEntityWithRelationships)

将对象添加到相关端。Adds an object to the related end.

(继承自 RelatedEnd)
IRelatedEnd.Add(Object)

将对象添加到相关端。Adds an object to the related end.

(继承自 RelatedEnd)
IRelatedEnd.Attach(IEntityWithRelationships)

在相关端处附加对象。Attaches an object at the related end.

(继承自 RelatedEnd)
IRelatedEnd.Attach(Object)

将对象附加到相关端。Attaches an object to the related end.

(继承自 RelatedEnd)
IRelatedEnd.CreateSourceQuery()

返回一个 IEnumerable,它表示属于相关端的对象。Returns an IEnumerable that represents the objects that belong to the related end.

(继承自 RelatedEnd)
IRelatedEnd.Remove(IEntityWithRelationships)

从相关端处的集合中移除一个对象。Removes an object from the collection at the related end.

(继承自 RelatedEnd)
IRelatedEnd.Remove(Object)

如果某个对象是相关端的一部分,则从该相关端中移除该对象。Removes an object from the related end if the object is part of the related end.

(继承自 RelatedEnd)

扩展方法

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个节点的上级。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

返回节点集合(其中包含源集合中的所有节点),并按文档顺序排列。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于