我是新来的LINQ,所以如果我问错了请道歉。我想使用帐户GUID和LINQ检索Dynamics CRM中的帐户号码。却找不到。下面的代码段给出了指定的cast无效的异常
accountID = (Guid)contact["parentcustomerid"];
var account = from a in context.CreateQuery("account")
where a.GetAttributeValue<Guid?>("accountid") == accountID
select new {accountNumber = a["accountnumber"] };
什么是accountID
?Guid
或Nullable<Guid>
try下面的代码片段:
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") };
可以使用Query Expression
。它非常容易使用:
QueryExpression queryExpression = new QueryExpression("account");
queryExpression.ColumnSet = new ColumnSet("accountnumber");
queryExpression.Criteria.AddCondition("accountid", ConditionOperator.Equal, accountID);
或
_service.Retrieve("account", accountID, new ColumnSet("accountnumber"));
尝试如下:
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();