[英]How do I retrieve a single record on Microsoft CRM using c# if I don't know the GUID?
We have a vendor solution here where it uses Microsoft Dynamics CRM as base. 我们这里有一个供应商解决方案,它使用Microsoft Dynamics CRM作为基础。 The application includes this custom entity which has the following attributes (truncated to only show the relevant bits)
该应用程序包括具有以下属性的该自定义实体(被截断以仅显示相关位)
relationship entity (works like a list of values. like values for populating a dropdown list) 关系实体 (像值列表一样工作。像用于填充下拉列表的值一样)
relationshipid (guid datatype. primary key)
description (string and sample values would include "staff" or "customer" or "visitor" and etc)
I want to retrieve a single record from the entity. 我想从实体中检索一条记录。 something like:
就像是:
select * from relationship where description like "staff%"
I know there's a Retrieve function but I need the guid to use that. 我知道有一个检索功能,但是我需要向导来使用它。 I want to emulate the SQL above without having to possibly use QueryExpression.
我想模拟上面的SQL,而不必使用QueryExpression。 I want to get an object of type Entity instead of EntityCollection which is what a QueryExpression will give me.
我想获取一个Entity类型而不是EntityCollection的对象,这是QueryExpression给我的。
thanks a lot :) 非常感谢 :)
The SDK doesn't provide a method to return a single entity unless you have its Id. 除非您具有其ID,否则SDK不会提供返回单个实体的方法。 But you can write an extension method to help yourself out.
但是您可以编写扩展方法来帮助自己。
Here is what I use: 这是我使用的:
/// <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();
}
The GetFirstOrDefault ensures that the QE is only going to retrieve one entity. GetFirstOrDefault确保QE仅检索一个实体。 The ToEntityList Casts each entity to the early bound type being returned.
ToEntityList将每个实体强制转换为要返回的早期绑定类型。
So the call would look like: 因此,呼叫看起来像:
var contact = service.GetFirstOrDefault<Contact>(qe);
just check if the EntityCollection
contains one element, if true returns the single element 只需检查
EntityCollection
包含一个元素,如果为true,则返回单个元素
EntityCollection results = service...
if (results.Entities.Count == 1) {
return results.Entities[0];
}
To add to the other answers, even though you will still get an EntityCollection in return to a query expression, you can specify a top count to only retrieve 1 record as an alternative. 要添加到其他答案中,即使仍然返回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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.