Skip to content

Instantly share code, notes, and snippets.

@ThatWilsonNerd
Last active August 29, 2015 14:14
Show Gist options
  • Save ThatWilsonNerd/4738eaa727fda266d4d1 to your computer and use it in GitHub Desktop.
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.
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();
}
}
}
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