问题
I'm retrieving the results after connecting to the dynamics CRM as below:
CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["default"].ConnectionString);
IOrganizationService crmService = crmConn.OrganizationServiceProxy;
QueryExpression query = new QueryExpression("opportunity");
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("name", ConditionOperator.Like, "14%");
EntityCollection results = crmService.RetrieveMultiple(query);
Now as the name is of type string, I was able to add condition & perform operations & get the results.
My requirement is I've to add another filter which is a lookup property "parentaccountid".
I'm trying to add the condition as below but it throws cast exception error as it is expecting only GUID.
query.Criteria.AddCondition("parentaccountid", ConditionOperator.Like, "%In%");
Note: the type of parentaccountid is Microsoft.Xrm.Sdk.EntityReference when I retrieved from early results
The reason is we can apply filter for parentaccountid only with GUID.
Is there any way to add the condition based on "Name" instead of "Id"?
回答1:
Yes, just add "name"
to the end of "parentaccountid"
:
query.Criteria.AddCondition("parentaccountidname", ConditionOperator.Like, "%In%");
You'll notice that if you create an Advanced Find like this:
And then download the FetchXML, CRM just appends "name"
to the end of lookup attribute name:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="opportunity">
<attribute name="name" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="parentaccountidname" operator="like" value="%In%" />
</filter>
</entity>
</fetch>
An alternative solution would be to use a LinkEntity
, and query the name of the linked account.
来源:https://stackoverflow.com/questions/45230749/querying-lookup-property-attribute-with-name-in-opportunity-entity-dynamics