user1603734
user1603734

Reputation: 1131

Error when using Linq to CRM with Early Bound Entities

I am writing a plugin in CRM 2011. I am trying to use Linq to CRM to retrieve an entity record, here is the code snippet:

Entity legalcase = new Entity("lgl_legalcase");
legalcase = legalDataContext.Lgl_legalcaseSet.FirstOrDefault(l => l.Lgl_legalcaseId == legalCaseGUID);

It is throwing an error on this line saying that it cannot convert from type Microsoft.Xrm.Sdk.Entity to type Legal.Entities.Lgl_legalcase. I have verified that this plugin works fine when using a Retrieve method instead of the LINQ syntax, but I would like to get it working with LINQ. Does anyone know why this is throwing an error?

Upvotes: 1

Views: 1194

Answers (2)

Mike_Matthews_II
Mike_Matthews_II

Reputation: 732

I just refactored my code such that the Proxies are now in a library referenced by my plugins; and after the refactor, I receive this error message.

After searching the internet for 20 seconds, I found the following suggested fix: add [assembly: Microsoft.Xrm.Sdk.Client.ProxyTypeAssemblyAttribute()] to the plugin assembly

My VM is acting up and won't allow me to Copy&Paste, otherwise, I would share the link from Microsoft social and credit the person who I copied this from.

Upvotes: 1

Guido Preite
Guido Preite

Reputation: 15128

You need to cast:

Entity legalcase = new Entity("lgl_legalcase")
legalcase = (Entity)legalDataContext.Lgl_legalcaseSet.FirstOrDefault(l => l.Lgl_legalcaseId == legalCaseGUID);

or define legalcase as Lgl_legalcase type

Lgl_legalcase legalcase = new Lgl_legalcase();
legalcase = legalDataContext.Lgl_legalcaseSet.FirstOrDefault(l => l.Lgl_legalcaseId == legalCaseGUID);

Upvotes: 1

Related Questions