1
votes

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?

2
For the moment, assume the error is valid...write .ToEntity<Lgl_legalcase>() at the end and see what happens. In plugins, this is often the solution. I won't go into why this is the case or other strategies for fixing it. - Mike_Matthews_II
I tried adding this to the end, but I still receive the same error. - user1603734
what do you get with var something = legalDataContext.Lgl_legalcaseSet.FirstOrDefault(l => l.Lgl_legalcaseId == legalCaseGUID) ? It was my assumption this would return a Microsoft.Xrm.Sdk.Entity type. - Mike_Matthews_II
Using the var something statement, I still receive the same error. - user1603734
if the answer I have submitted below answers your question, please mark it as the answer; otherwise, please submit more information. Thanks. - Mike_Matthews_II

2 Answers

1
votes

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);
1
votes

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.