From 0fdde94566e36a28cb5dfd776223f0418b470efd Mon Sep 17 00:00:00 2001 From: Daniel Svensson Date: Tue, 13 Feb 2024 12:54:03 +0100 Subject: [PATCH] Skip OfType in EntityRef.GetSingleMatch if possible --- src/OpenRiaServices.Client/Framework/EntityRef.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/OpenRiaServices.Client/Framework/EntityRef.cs b/src/OpenRiaServices.Client/Framework/EntityRef.cs index cc6dfc0cc..9d89d3481 100644 --- a/src/OpenRiaServices.Client/Framework/EntityRef.cs +++ b/src/OpenRiaServices.Client/Framework/EntityRef.cs @@ -223,8 +223,11 @@ private bool Filter(TEntity entity) /// The entity or null. private TEntity GetSingleMatch(IEnumerable entities) { + IEnumerable enumerable = (entities as ICollection) + ?? entities.OfType(); + TEntity entity = null; - foreach (TEntity currEntity in entities.OfType().Where(this.Filter)) + foreach (TEntity currEntity in enumerable.Where(this.Filter)) { if (entity != null) { @@ -281,8 +284,8 @@ private void MonitorEntitySet() /// The collection changed event arguments. 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)) @@ -294,7 +297,7 @@ private void SourceSet_CollectionChanged(object sender, NotifyCollectionChangedE } else if (args.Action == NotifyCollectionChangedAction.Remove) { - if (this._entity != null && args.OldItems.OfType().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