I'm new to LINQ so please apology if I ask wrong question. I want to retrieve account number in Dynamics CRM using account GUID and LINQ. But unable to find. I've following snippet but gives me exception that specified cast is not valid
accountID = (Guid)contact["parentcustomerid"];
var account = from a in context.CreateQuery("account")
where a.GetAttributeValue<Guid?>("accountid") == accountID
select new {accountNumber = a["accountnumber"] };
What is accountID
? Guid
or Nullable<Guid>
try below snippet:
accountID = (Guid)contact["parentcustomerid"];
var account = from a in context.CreateQuery("account")
where a.GetAttributeValue<Guid>("accountid") == accountID
select new {accountNumber = a.GetAttributeValue<int>("accountnumber") };
you can use Query Expression
. It is very easy to use:
QueryExpression queryExpression = new QueryExpression("account");
queryExpression.ColumnSet = new ColumnSet("accountnumber");
queryExpression.Criteria.AddCondition("accountid", ConditionOperator.Equal, accountID);
Or
_service.Retrieve("account", accountID, new ColumnSet("accountnumber"));
Try something like following:
var accountID = contact.GetAttributeValue<EntityReference>("parentcustomerid").Id;
var accountnumber = (from a in context.CreateQuery("account")
where a.GetAttributeValue<Guid>("accountid") == accountID
select a.GetAttributeValue<string>("accountnumber")).FirstOrDefault();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.