我正在尝试从Dynamics CRM 2013工作流中的相关CRM实体获取字段。以下代码返回映射的实体,但未引用该实体中的匹配记录。
如何更新它,以便它对两个匹配的实体Guid运行以下查询?我需要使用EntityReference吗?
QueryExpression FindAddressLots = new QueryExpression(context.PrimaryEntityName);
FindAddressLots.LinkEntities.Add
(new LinkEntity(context.PrimaryEntityName, "appdetails_data", "applicationdetailsid", "appdetails_dataid", JoinOperator.Inner));
FindAddressLots.LinkEntities[0].Columns.AddColumns("appdetails_dataid", "suburb", "postcode");
FindAddressLots.LinkEntities[0].EntityAlias = "lotdata";
EntityCollection ec = service.RetrieveMultiple(FindAddressLots);
foreach (Entity act in ec.Entities)
{
if (act.Attributes.Contains("lotdata.suburb"))
{
FindAddressLots = act.GetAttributeValue<AliasedValue>("lotdata.suburb").Value.ToString();
}
}
}
有什么建议?这将不胜感激。谢谢。
首先让我们澄清一件事:我们不知道字段的类型suburb
,在我的代码中,我将假定它是一个字符串,但可以是其他字符串。
可以按以下方式重写查询代码(链接属性,列和别名相同):
QueryExpression queryAddressLots = new QueryExpression(context.PrimaryEntityName);
LinkEntity linkDetails = queryAddressLots.AddLink("appdetails_data", "applicationdetailsid", "appdetails_dataid", JoinOperator.Inner);
linkDetails.Columns.AddColumns("appdetails_dataid", "suburb", "postcode");
linkDetails.EntityAlias = "lotdata";
EntityCollection collAddressLots = service.RetrieveMultiple(queryAddressLots);
根据代码,没有逻辑来检索特定记录(但通常存在),否则它将返回所有Address Lots
与明细实体连接的实体(我之前也写过注释,没有人关心真实的实体名称,但是它们应该是确切的说,当我阅读一个名为applicationdetailsid
people的字段时,可以假设存在一个称为的实体,applicationdetails
而该实体不存在
第二部分是如何获取值,可以将其重写为(也在这里,变量名,称为act
或ec
不应使用的名称):
foreach (Entity addressLot in collAddressLots.Entities)
{
AliasedValue aliasSuburb = addressLot.GetAttributeValue<AliasedValue>("lotdata.suburb");
if (aliasSuburb != null && aliasSuburb.Value != null)
{
string suburb = (string)aliasSuburb.Value;
}
}
请注意,我对字符串进行了强制转换,因为正如我在假定字段是字符串之前写的那样,如果该字段是a,Whole Number
我可能会做类似Convert.ToInt32(aliasSuburb.Value)
现在,问题提到了it isn't referencing the matching record in the entity
,我认为它是指这样的事实(正如我之前写的那样):没有提供ID,并且返回了所有记录。这可以通过在QueryExpression
定义之后添加条件来轻松解决,例如:
queryAddressLots.Criteria.AddCondition("applicationdetailsid", ConditionOperator.Equal, context.PrimaryEntityId);
当然,我们不能保证这是否可行,因为我们不知道字段名称,希望作者知道。老实说,我们可以查询这个appdetails_data
实体并添加一个条件以匹配applicationdetailsid
(对我来说,它看起来像是N:1关系,而不是1:N关系),suburb
而无需使用a来查询字段,LinkEntity
但我不能100%肯定。
希望对你有所帮助,下次,请不要隐藏实体和字段名称,只需更改前缀即可。
嗨,圭多。非常感谢您的回复,我再次更新了我的原始代码以反映您的建议。这肯定链接了实体,但是当我应用过滤器时,我没有得到任何实体返回。
这肯定链接了实体,但是当我应用过滤器时,没有实体返回。-> queryAddressLots.Criteria.AddCondition(“ applicationdetailsid”,ConditionOperator.Equal,context.PrimaryEntityId); appdetailsid应该= company_appdetails_subdivid。我的主要实体的ID是与位于company_appdetails_subdivId中的GUID不同的GUID。
我收到此代码“ company_mainapplication”实体不包含名称为“ company_appdetails_subdivid”的属性的以下错误。这是正确的,我希望映射到company_mainapplication的实体是company_applicationdetailsid
感谢Guido,您的解决方案有效。我只需要更新即可使用正确的ID。