C# 如果我不知道GUID,如何使用c在Microsoft CRM上检索单个记录?

C# 如果我不知道GUID,如何使用c在Microsoft CRM上检索单个记录?,c#,dynamics-crm-2011,C#,Dynamics Crm 2011,我们这里有一个供应商解决方案,它使用Microsoft Dynamics CRM作为基础。应用程序包括此自定义实体,该实体的以下属性被截断,仅显示相关位 关系实体的工作方式类似于值列表。类似于填充下拉列表的值 relationshipid (guid datatype. primary key) description (string and sample values would include "staff" or "customer" or "visitor" and etc) 我想从实

我们这里有一个供应商解决方案,它使用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%"
我知道有一个检索函数,但我需要guid来使用它。我想模拟上面的SQL,而不必使用QueryExpression。我想得到一个Entity类型的对象,而不是EntityCollection,这是QueryExpression将提供给我的

非常感谢:

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

EntityCollection results = service...
if (results.Entities.Count == 1) {
   return results.Entities[0];
}
只需检查EntityCollection是否包含一个元素,如果为true,则返回单个元素

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

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

以下是我使用的:

/// ///获取与查询表达式匹配的第一个实体。如果找不到,则返回Null。 /// ///实体类型。 ///服务。 ///查询表达式。 /// public static T getfirst或defaultthis IOrganizationService服务,QueryExpression qe其中T:Entity { 第一,; return service.RetrieveMultipleqe.ToEntityList.FirstOrDefault; } /// ///将实体集合转换为列表,强制转换每个实体。 /// ///实体的类型 ///要转换的集合 /// 公共静态列表到实体列表此实体集合列,其中T:Entity { 返回col.Entities.Selecte=>e.ToEntity.ToList; } GetFirstOrDefault确保QE只检索一个实体。ToEntityList将每个实体强制转换为返回的早期绑定类型

所以这个电话看起来像:

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

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

以下是我使用的:

/// ///获取与查询表达式匹配的第一个实体。如果找不到,则返回Null。 /// ///实体类型。 ///服务。 ///查询表达式。 /// public static T getfirst或defaultthis IOrganizationService服务,QueryExpression qe其中T:Entity { 第一,; return service.RetrieveMultipleqe.ToEntityList.FirstOrDefault; } /// ///将实体集合转换为列表,强制转换每个实体。 /// ///实体的类型 ///要转换的集合 /// 公共静态列表到实体列表此实体集合列,其中T:Entity { 返回col.Entities.Selecte=>e.ToEntity.ToList; } GetFirstOrDefault确保QE只检索一个实体。ToEntityList将每个实体强制转换为返回的早期绑定类型

所以这个电话看起来像:

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

要添加到其他答案中,即使您仍将获得EntityCollection作为查询表达式的返回,也可以指定top count以仅检索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");

}

要添加到其他答案中,即使您仍将获得EntityCollection作为查询表达式的返回,也可以指定top count以仅检索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");

}

我希望像Retrieve这样简单的东西,因为设置EntityCollection需要几行代码。但我确实看到了[0]在搜索中的作用。谢谢:我希望像检索一样简单,因为设置EntityCollection需要几行代码。但我确实看到了[0]在搜索中的作用。谢谢: