1

我经常使用QueryExpression,但到目前为止,它是一个直接的 get-this-from-that 或 put-this-in-that。最近,我了解到有一个叫做LinkedEntity的东西,我开始寻找它。作为一个例子,我受到 SO 上的一个相关问题的启发,我开始创建一个表达式来获取列表中的所有成员,因为它是 guid。

不过,我发现的所有示例都遵循相同的模式 - 正如本示例所示。从这个问题中,我了解到这是一种过时的方法(CRM 4.0)。我未能找到更新的示例,我不确定如何设计链接。

有人愿意提供示例代码吗?

Guid guid = ...;
QueryExpression request = new QueryExpression
{
  EntityName = "account",
  ColumnSet = new ColumnSet(true),
  LinkEntities= ???,                        // How to link the entities correctly?
  Criteria = new FilterExpression { ??? }   // How to filter for *guid* only?
};

我创建了一个链接两个实体的 fetch-XML,但我不清楚如何将其转换为QueryExpression实体。我有这样的东西。有什么建议么?

LinkEntity linkListToMember = new LinkEntity(
  "list", "listmember", "listid", "listid", JoinOperator.Natural);
LinkEntity linkMemberToContact = new LinkEntity(
  "listmember", "account", "entityid", "accountid", JoinOperator.Natural);
4

2 回答 2

1

链接实体用作您的 SQL 连接。使用带有 from 和 to 实体和属性名称的构造函数

public LinkEntity(
    string linkFromEntityName, // This is the Entity Logical Name of your Query Expression, or parent LinkEntity
    string linkToEntityName, // This is the Entity Logical Name of the entity you'd like to link to
    string linkFromAttributeName, // This is the attribute name on your from entity, containing your join key
    string linkToAttributeName, // This is the attribute name on your to entity, containing your join key
    JoinOperator joinOperator) // This is the type of Join you'd like to perform

使用链接实体,您可以添加链接条件来过滤返回的结果。您还可以添加列并从相关实体返回数据。

编辑,康拉德答案的补充

如果 Konrad 列出的 52 行代码看起来过于冗长,这将使用此处定义的扩展方法在 15 行中完成相同的操作。

Guid guid = ...;
IOrganizationService service;

QueryExpression request = new QueryExpression("account")
{
  ColumnSet = new ColumnSet("name", "region"),
};
request.Criteria.AddCondition("name", ConditionOperator.NotNull);
request.Criteria.AddCondition("region", ConditionOperator.NotNull);

var listLink = request.AddLink("listmember", "accountid", "entityid").
  AddChildLink("list", "listid");

listLink.Columns.AddColumn("listname");
listLink.LinkCriteria.AddCondition("listid", ConditionOperator.Equal, guid);
于 2013-01-14T16:25:47.960 回答
0

这是获取营销列表所有成员的方法,前提是您有它的 guid 和服务器连接。您对条件所做的事情是正确的,您需要将一个插入另一个。星期六我会在我的博客上放一个更大的描述。

Guid guid = ...;
IOrganizationService service;

QueryExpression request = new QueryExpression
{
  EntityName = "account",
  ColumnSet = new ColumnSet("name", "region"),
  LinkEntities =
  {
    new LinkEntity 
    { 
      JoinOperator = JoinOperator.Inner, 
      LinkFromEntityName = "account", 
      LinkFromAttributeName = "accountid", 
      LinkToEntityName = "listmember", 
      LinkToAttributeName = "entityid",
      LinkCriteria = { },
      LinkEntities =
      {
        new LinkEntity
        {
          JoinOperator = JoinOperator.Inner,
          Columns = new ColumnSet("listname"),
          EntityAlias = "MarketingList",
          LinkFromEntityName = "listmember",
          LinkFromAttributeName = "listid",
          LinkToEntityName = "list",
          LinkToAttributeName = "listid",
          LinkCriteria = { Conditions = 
          {
            new ConditionExpression("listid", ConditionOperator.Equal, guid) 
          } }
        }
      }
    } 
  },
  Criteria = new FilterExpression
  {
    Filters =
    {
      new FilterExpression
      {
        FilterOperator = LogicalOperator.And,
        Conditions =
        {
          new ConditionExpression("name", ConditionOperator.NotNull),
          new ConditionExpression("region", ConditionOperator.NotNull)
        }
      }
    }
  }
};

然后,当然你需要执行调用。

EntityCollection result = service.RetrieveMultiple(request);

最后,您可能想要订购和构建您从服务器获得的任何内容。我正在使用以下 LINQ-to-Data 表达式。

IEnumerable<Member> output = result.Entities.Where(element 
  => Member.IsWellFormed(element)).Select(element
    => new Member(element));

有关该主题的更多信息,请参阅博客。

于 2013-01-15T07:20:42.630 回答