Hosk CRM Dev Tip – Get EntityReference from Entity

 This is probably a tip a lot of people have been using forever but for some reason I only found this functionality a couple of weeks ago.

A lot of times in the code you need to create an EntityReference to pass to another method, use in a query expression.
I often use to use this code
EntityReference oldEntRef = new EntityReference (“account” , entity.Id);

this worked fine but it was a bit of extra typing and if I was using code which wasn’t early bound then I would have to look up the logicalname of the entity, which can be tricky for custom entities.

but then I found this easy method.

EntityReference entRef = entity.ToEntityReference();