简体   繁体   English

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

[英]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.

相关问题 我不知道如何使用 C# ASP.NET 进行 Soap 请求 - I don't know how to do a Soap Request using C# ASP.NET 如何在Microsoft Dynamics / CRM中使用C#/ SDK将帐户关联到联系人? - How do I associate an account to a contact using C#/SDK in Microsoft Dynamics/CRM? 当我事先不知道密钥时,如何在 C# 中解析 JSON 对象? - How do I parse a JSON object in C# when I don't know the key in advance? 当我不知道键和属性没有名称时,如何在 C# 中解析 JSON 对象? - How do I parse a JSON object in C# when I don't know key and properties don't have names? 我需要做一个 python 项目,但我知道 C# 并且不知道如何转换它 - I need to make a python project but I know C# and don't know how to convert it 如何使用MS动态CRM 2016中的c#检索部分,选项卡? - How can I retrieve sections, tabs by using c# from MS dynamics CRM 2016? 如何使用 .NET 以编程方式获取 C# 中应用程序的 GUID - How do I programmatically get the GUID of an application in C# with .NET C#NullReferenceException,但我不知道是什么原因引起的 - C# NullReferenceException, but I don't know what's causing it 如何比较两个 IEnumerable<T> 在 C# 中,如果我不知道实际的对象类型? - How to compare two IEnumerable<T> in C# if I don't know the actual object type? 如何在C#中使用Reflection检索对子对象的引用? - How do I retrieve a reference to a subobject using Reflection in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM