Last active
August 29, 2015 14:14
-
-
Save ThatWilsonNerd/4738eaa727fda266d4d1 to your computer and use it in GitHub Desktop.
Quick IEqualityComparer class to help determine if an EntityCollection object contains an entity with the same id as the provided entity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Microsoft.Crm.Sdk; | |
using Microsoft.Crm.Sdk.Messages; | |
using Microsoft.Xrm.Sdk; | |
using Microsoft.Xrm.Sdk.Query; | |
namespace PluginUtils | |
{ | |
class EntityComparer : IEqualityComparer<Entity> | |
{ | |
public bool Equals(Entity e1, Entity e2) | |
{ | |
return e1.Id == e2.Id; | |
} | |
public int GetHashCode(Entity e) | |
{ | |
return e.GetHashCode(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EntityCollection records = service.RetrieveMultiple(qry); | |
if (records.Entities.Count > 0 && !records.Entities.Contains(new Entity("contact") { id = "my guid" },new EntityComparer())) | |
{ | |
// throw exception | |
throw new InvalidPluginExecutionException(OperationStatus.Canceled, "The specified record is not in the collection."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment