【问题标题】:How do I retrieve a single record on Microsoft CRM using c# if I don't know the GUID?如果我不知道 GUID,如何使用 c# 在 Microsoft CRM 上检索单个记录?
【发布时间】:2014-06-19 07:38:49
【问题描述】:

我们这里有一个供应商解决方案,它使用 Microsoft Dynamics CRM 作为基础。该应用程序包含此自定义实体,该实体具有以下属性(截断以仅显示相关位)

关系实体(类似于值列表。类似于填充下拉列表的值)

relationshipid (guid datatype. primary key)
description (string and sample values would include "staff" or "customer" or "visitor" and etc)

我想从实体中检索一条记录。类似:

select * from relationship where description like "staff%"

我知道有一个 Retrieve 函数,但我需要 guid 才能使用它。我想在不必使用 QueryExpression 的情况下模拟上面的 SQL。我想要获取 Entity 类型的对象,而不是 QueryExpression 将提供给我的 EntityCollection。

非常感谢:)

【问题讨论】:

    标签: c# dynamics-crm-2011


    【解决方案1】:

    SDK 不提供返回单个实体的方法,除非您有其 ID。但是你可以编写一个扩展方法来帮助自己。

    这是我使用的:

    /// <summary>
    /// Gets the first entity that matches the query expression.  Null is returned if none are found.
    /// </summary>
    /// <typeparam name="T">The Entity Type.</typeparam>
    /// <param name="service">The service.</param>
    /// <param name="qe">The query expression.</param>
    /// <returns></returns>
    public static T GetFirstOrDefault<T>(this IOrganizationService service, QueryExpression qe) where T : Entity
    {
        qe.First();
        return service.RetrieveMultiple(qe).ToEntityList<T>().FirstOrDefault();
    }
    
    
    /// <summary>
    /// Converts the entity collection into a list, casting each entity.
    /// </summary>
    /// <typeparam name="T">The type of Entity</typeparam>
    /// <param name="col">The collection to convert</param>
    /// <returns></returns>
    public static List<T> ToEntityList<T>(this EntityCollection col) where T : Entity
    {
        return col.Entities.Select(e => e.ToEntity<T>()).ToList();
    }
    

    GetFirstOrDefault 确保 QE 只会检索一个实体。 ToEntityList 将每个实体强制转换为要返回的早期绑定类型。

    所以调用看起来像:

    var contact = service.GetFirstOrDefault<Contact>(qe);
    

    【讨论】:

      【解决方案2】:

      只检查EntityCollection是否包含一个元素,如果为真则返回单个元素

      EntityCollection results = service...
      if (results.Entities.Count == 1) {
         return results.Entities[0];
      }
      

      【讨论】:

      • 我希望像 Retrieve 这样简单的东西,因为它设置 EntityCollection 使用了几行。但我确实在搜索中看到了 [0] 的使用。谢谢:)
      【解决方案3】:

      要添加到其他答案,即使您仍然会得到一个 EntityCollection 作为查询表达式的返回值,您也可以指定一个最高计数以仅检索 1 条记录作为替代。

      QueryExpression qeOpportunity = new QueryExpression();
      qeOpportunity.EntityName = "opportunity";
      qeOpportunity.ColumnSet = new ColumnSet(new string[] { "sp_shippingmethod_lookup", "description" });
      qeOpportunity.TopCount = 1;
      // Add other Conditions here with qeOpportunity.Criteria.AddCondition()....
      
      EntityCollection ecOpportunity = service.RetrieveMultiple(qeOpportunity);
      
      if (ecOpportunity.Entities.Count > 0)
      {
          string description = ecOpportunity.Entities[0].GetAttributeValue<string>("description");
          EntityReference myShippingMethod = ecOpportunity.Entities[0].GetAttributeValue<EntityReference>("sp_shippingmethod_lookup");
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-07
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多