Share via


EntityCollection<TEntity> Class

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

Represents a collection of associated entities.

Inheritance Hierarchy

System.Object
  System.ServiceModel.DomainServices.Client.EntityCollection<TEntity>

Namespace:  System.ServiceModel.DomainServices.Client
Assembly:  System.ServiceModel.DomainServices.Client (in System.ServiceModel.DomainServices.Client.dll)

Syntax

'Declaration
Public NotInheritable Class EntityCollection(Of TEntity As Entity) _
    Implements IEnumerable(Of TEntity), IEnumerable, INotifyCollectionChanged,  _
    INotifyPropertyChanged, ICollectionViewFactory
'Usage
Dim instance As EntityCollection(Of TEntity)
public sealed class EntityCollection<TEntity> : IEnumerable<TEntity>, 
    IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged, ICollectionViewFactory
where TEntity : Entity
generic<typename TEntity>
where TEntity : Entity
public ref class EntityCollection sealed : IEnumerable<TEntity>, 
    IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged, ICollectionViewFactory
[<SealedAttribute>]
type EntityCollection<'TEntity when 'TEntity : Entity> =  
    class
        interface IEnumerable<'TEntity>
        interface IEnumerable
        interface INotifyCollectionChanged
        interface INotifyPropertyChanged
        interface ICollectionViewFactory
    end
JScript does not support generic types and methods.

Type Parameters

  • TEntity
    The type of Entity in the collection.

The EntityCollection<TEntity> type exposes the following members.

Constructors

  Name Description
Public method EntityCollection<TEntity>(Entity, String, Func<TEntity, Boolean>) Initializes a new instance of the EntityCollection<TEntity> class with the specified parent, name of the collection on the parent, and filter function.
Public method EntityCollection<TEntity>(Entity, String, Func<TEntity, Boolean>, Action<TEntity>, Action<TEntity>) Initializes a new instance of the EntityCollection<TEntity> class with the specified parent, name of the collection on the parent, and functions for filtering, attaching, and detaching.

Top

Properties

  Name Description
Public property Count Gets the current count of entities in this collection.

Top

Methods

  Name Description
Public method Add Adds the specified entity to this collection.
Public method Equals (Inherited from Object.)
Protected method Finalize (Inherited from Object.)
Public method GetEnumerator Returns an enumerator for this collection.
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Protected method MemberwiseClone (Inherited from Object.)
Public method Remove Removes the specified entity from this collection.
Public method ToString Returns a textual representation of the collection. (Overrides Object.ToString().)

Top

Events

  Name Description
Public event EntityAdded Raised when an Entity is added to this collection.
Public event EntityRemoved Raised when an Entity is removed from this collection.

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate event INotifyCollectionChanged.CollectionChanged Raised when the contents of the collection changes.
Explicit interface implemetationPrivate method ICollectionViewFactory.CreateView Returns a custom view for specialized sorting, filtering, grouping, and currency.
Explicit interface implemetationPrivate method IEnumerable.GetEnumerator Returns an enumerator for this collection.
Explicit interface implemetationPrivate event INotifyPropertyChanged.PropertyChanged Raised when a property on this collection changes.

Top

Examples

' <summary>
' Gets the collection of associated <see cref="Employee"/> entity instances.
' </summary>
<Association("Employee_Employee", "EmployeeID", "ManagerID")>
<XmlIgnore()>
Public ReadOnly Property Employee0 As EntityCollection(Of Employee)
    Get
        If Me._employee0 = Nothing Then
            Me._employee0 = New EntityCollection(Of Employee)(Me, "Employee0", Me.FilterEmployee0);
        End If
    End Get
End Property

<Association("Employee_Employee", "EmployeeID", "ManagerID")>
<XmlIgnore()>
Public ReadOnly Property Employee1 As EntityCollection(Of Employee)
    Get
        If Me._employee1 = Nothing Then
            Me._employee1 = New EntityCollection(Of Employee)(Me, "Employee1", Me.FilterEmployee1, Me.AttachEmployee1, Me.DetachEmployee1);
        End If
    End Get
End Property

' <summary>
' Gets or sets the associated <see cref="Employee"/> entity.
' </summary>
<Association("Employee_Employee", "ManagerID", "EmployeeID", IsForeignKey = True)>
<XmlIgnore()>
Public Property Employee2 As Employee
    Get
        If Me._employee2 = Nothing Then
            Me._employee2 = New EntityRef(Of Employee)(Me, "Employee2", Me.FilterEmployee2)
        End If
    End Get
    Set(ByVal value As Employee)
        Dim previous As Employee = Me.Employee2

        If previous = Me.Employee2 Then
            Me.ValidateProperty("Employee2", value)
            ' EntityCollection.Remove()
            If previous <> Value Then
                Me._employee2.Entity = Nothing
                previous.Employee1.Remove(Me)
                Me.Employee1.EntityRemoved += New EventHandler(Of EntityCollectionChangedEventArgs(Of Employee))(Employee1_EntityRemoved)
            End If
            If Value <> Nothing Then
                Me.ManagerID = value.EmployeeID
            End If

            Me._employee2.Entity = value
            ' EntityCollection.Add()
            If value <> Nothing Then
                value.Employee1.Add(Me)
                Me.Employee1.EntityAdded += New EventHandler(Of EntityCollectionChangedEventArgs(Of Employee))(Employee1_EntityAdded)
            End If

            Me.RaisePropertyChanged("Employee2")
        End If
    End Set
End Property

Public Property Employee1_EntityAdded As EventHandler(Of EntityCollectionChangedEventArgs(Of Employee))
    Set(ByVal value As EventHandler(Of EntityCollectionChangedEventArgs(Of Employee)))
        Me._count = Me.Employee1.Count
    End Set
End Property

Public Property Employee1_EntityRemoved As EventHandler(Of EntityCollectionChangedEventArgs(Of Employee))
    Set(ByVal value As EventHandler(Of EntityCollectionChangedEventArgs(Of Employee)))
        Me._count = Me.Employee1.Count
    End Set
End Property

Public ReadOnly Property Employee1Enumerator As IEnumerator(Of Employee)
    Get
        Return this._employee1Enumerator
    End Get
End Property

Private Function AttachEmployee0(ByVal entity As Employee)
    entity.Employee2 = Me
End Function

Private Function AttachEmployee1(ByVal entity As Employee)
    entity.Employee2 = Me
End Function

Private Function DetachEmployee0(ByVal entity As Employee)
    entity.Employee2 = Nothing
End Function

Private Function DetachEmployee1(ByVal entity As Employee)
    entity.Employee2 = Nothing
End Function

Private Function FilterEmployee0(ByVal entity As Employee) As Boolean
    Return (entity.ManagerID = Me.EmployeeID)
End Function

Private Function FilterEmployee1(ByVal entity As Employee) As Boolean
    Return (entity.ManagerID = Me.EmployeeID)
End Function

Private Function FilterEmployee2(ByVal entity As Employee) As Boolean
    Return (entity.ManagerID = Me.EmployeeID)
End Function

/// <summary>
/// Gets the collection of associated <see cref="Employee"/> entity instances.
/// </summary>
[Association("Employee_Employee", "EmployeeID", "ManagerID")]
[XmlIgnore()]
public EntityCollection<Employee> Employee0
{
    get
    {
        if ((this._employee0 == null))
        {
            this._employee0 = new EntityCollection<Employee>(this, "Employee0", this.FilterEmployee0);
        }
        return this._employee0;
    }
}

[Association("Employee_Employee", "EmployeeID", "ManagerID")]
[XmlIgnore()]
public EntityCollection<Employee> Employee1
{
    get
    {
        if ((this._employee1 == null))
        {
            this._employee1 = new EntityCollection<Employee>(this, "Employee1", this.FilterEmployee1, this.AttachEmployee1, this.DetachEmployee1);
        }
        return this._employee1;
    }
}

/// <summary>
/// Gets or sets the associated <see cref="Employee"/> entity.
/// </summary>
[Association("Employee_Employee", "ManagerID", "EmployeeID", IsForeignKey = true)]
[XmlIgnore()]

public Employee Employee2
{
    get
    {
        if ((this._employee2 == null))
        {
            this._employee2 = new EntityRef<Employee>(this, "Employee2", this.FilterEmployee2);
        }
        return this._employee2.Entity;
    }
    set
    {
        Employee previous = this.Employee2;
        if ((previous != value))
        {
            this.ValidateProperty("Employee2", value);
            // EntityCollection.Remove()
            if ((previous != null))
            {
                this._employee2.Entity = null;
                previous.Employee1.Remove(this);
                this.Employee1.EntityRemoved += new EventHandler<EntityCollectionChangedEventArgs<Employee>>(Employee1_EntityRemoved);
            }
            if ((value != null))
            {
                this.ManagerID = value.EmployeeID;
            }
            else
            {
                this.ManagerID = default(Nullable<int>);
            }
            this._employee2.Entity = value;
            // EntityCollection.Add()
            if ((value != null))
            {
                value.Employee1.Add(this);
                this.Employee1.EntityAdded += new EventHandler<EntityCollectionChangedEventArgs<Employee>>(Employee1_EntityAdded);

            }
            this.RaisePropertyChanged("Employee2");
        }
    }
}

EventHandler<EntityCollectionChangedEventArgs<Employee>> Employee1_EntityAdded
{
    set
    {
        this._count = this.Employee1.Count;
    }

}


EventHandler<EntityCollectionChangedEventArgs<Employee>> Employee1_EntityRemoved
{
    set
    {
        this._count = this.Employee1.Count;
    }
}

public IEnumerator<Employee> Employee1Enumerator
{
    get
    {
        return this._employee1Enumerator;
    }
}

private void AttachEmployee1(Employee entity)
{
    entity.Employee2 = this;
}

private void DetachEmployee1(Employee entity)
{
    entity.Employee2 = null;
}

private bool FilterEmployee1(Employee entity)
{
    return (entity.ManagerID == this.EmployeeID);
}

private bool FilterEmployee2(Employee entity)
{
    return (entity.EmployeeID == this.ManagerID);
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.ServiceModel.DomainServices.Client Namespace