Skip to content

Commit

Permalink
Skip OfType in EntityRef.GetSingleMatch if possible (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Svensson authored Feb 14, 2024
1 parent d9710a1 commit e89b39f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/OpenRiaServices.Client/Framework/EntityRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,11 @@ private bool Filter(TEntity entity)
/// <returns>The entity or null.</returns>
private TEntity GetSingleMatch(IEnumerable entities)
{
IEnumerable<TEntity> enumerable = (entities as ICollection<TEntity>)
?? entities.OfType<TEntity>();

TEntity entity = null;
foreach (TEntity currEntity in entities.OfType<TEntity>().Where(this.Filter))
foreach (TEntity currEntity in enumerable.Where(this.Filter))
{
if (entity != null)
{
Expand Down Expand Up @@ -281,8 +284,8 @@ private void MonitorEntitySet()
/// <param name="args">The collection changed event arguments.</param>
private void SourceSet_CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (this._parent.EntityState != EntityState.New &&
args.Action == NotifyCollectionChangedAction.Add)
if (args.Action == NotifyCollectionChangedAction.Add
&& this._parent.EntityState != EntityState.New)
{
TEntity newEntity = this.GetSingleMatch(args.NewItems);
if ((newEntity != null) && (newEntity != this._entity))
Expand All @@ -294,7 +297,7 @@ private void SourceSet_CollectionChanged(object sender, NotifyCollectionChangedE
}
else if (args.Action == NotifyCollectionChangedAction.Remove)
{
if (this._entity != null && args.OldItems.OfType<TEntity>().Contains(this._entity))
if (this._entity != null && args.OldItems.Contains(this._entity))
{
// if the referenced Entity has been removed from the source EntitySet,
// we need to clear out our cached reference and raise the notification
Expand Down

0 comments on commit e89b39f

Please sign in to comment.