如何在 Dynamics 365 SDK 中检索没有 Guid 的实体?

How to retrieve a Entity without Guid in Dynamics 365 SDK?

嗯,我有这个命令:IOrganizationService.Retrieve(String entityName, Guid ID, ColumnSet columnSet)参考:IOrganizationService

我尝试了 SDK 代码示例,但在创建记录时使用 Guid 获取信息,如下所示:Guid _accountId = orgService.Create(account);

我想使用帐户(实体)的名称获取此 Guid,我该怎么做?

我使用查询得到了解决方案:

        //Query for the GUID of the Account using Account Name
        QueryExpression query = new QueryExpression("account");
        string[] cols = { "accountid", "name" };
        query.Criteria = new FilterExpression();
        query.Criteria.AddCondition("name", ConditionOperator.Equal, "Account Name");
        query.ColumnSet = new ColumnSet(cols);
        var account = Service.RetrieveMultiple(query);
        //Casting the reference to GUID
        Guid accountId = (Guid)account[0].Attributes["accountid"];

这将return使用帐户名的Guid信息。

您必须使用 RetrieveMultiple 方法 & QueryExpression 来实现它。例如,下面的 MSDN code example 是自我解释的,以获取与 lastname = “Brown”

的联系人
//  Query using ConditionExpression and FilterExpression
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "lastname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values.Add("Brown");            

FilterExpression filter1 = new FilterExpression();
filter1.Conditions.Add(condition1);

QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname");
query.Criteria.AddFilter(filter1);

EntityCollection result1 = _serviceProxy.RetrieveMultiple(query);
Console.WriteLine();Console.WriteLine("Query using Query Expression with ConditionExpression and FilterExpression");
Console.WriteLine("---------------------------------------");
foreach (var a in result1.Entities)
{
    Console.WriteLine("Name: " + a.Attributes["firstname"] + " " + a.Attributes["lastname"]);
}

Query expressions are used in methods that retrieve more than one record, such as the IOrganizationService.RetrieveMultiple method, in messages that perform an operation on a result set specified by a query expression, such as BulkDeleteRequest and when the ID for a specific record is not known.

Reference

查询表达式是一种选择。我发现它是效率最低的。寻找那些更常用的方法: