How to retieve CRM Guid using LINQ and joins?

会有一股神秘感。 提交于 2019-12-11 04:05:51

问题


We are using CRM 2011. We have Contracts with Entity Reference to Product and each Product has an Entity Reference to a Subject. Given a Contract Guid, I need to retrieve the Subject Guid.

I am a beginner at LINQ but I coded:

 var subject = from s in context.SubjectSet
                                       join product in context.ProductSet
                                           on s.Id equals product.SubjectId.Id
                                       join contract in context.ContractSet
                                           on product.Id equals contract.ce_ProductId.Id
                                       where contract.Id == gContractId
                                       select s;

                foreach (var s in subject)
                {
                    newReportableAction.ce_SupergroupRegarding =
                    new EntityReference(Xrm.Subject.EntityLogicalName, new Guid(s.Id.ToString()));
                }

This throws an error:

AttributeFrom and AttributeTo must be either both specified or both ommited. You can not pass only one or the other. AttributeFrom: , AttributeTo: ce_ProductId

What does this error mean?
How can I get the Guid?

Update:

I tried breaking the query into parts to see where the error was being generated from so I had:

var query = from product in context.ProductSet
                            join contract in context.ContractSet
                            on product.Id equals contract.ce_ProductId.Id

This gives:

"The type of one of expressions in the join clause is incorrect. Type inference failed in the call to 'Join'"

Thank you to all who help...


回答1:


I don't think you can use the .Id property right off the entity in Linq statements. Try this instead:

var query = from product in context.ProductSet
               join contract in context.ContractSet
               on product.ProductId equals contract.ce_ProductId.Id

Notice product.ProductId instead of product.Id.



来源:https://stackoverflow.com/questions/23373931/how-to-retieve-crm-guid-using-linq-and-joins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!