如何识别实体的类型?

问题描述 投票:0回答:4

如果我有记录的GUID,但我不知道它是帐户还是联系人,我该如何检索此记录并识别其类型?

例如,我可以像这样检索指定的类型:

        var serviceAppointment = organizationService.Retrieve(
            "serviceappointment",
            serviceActivityGuid,
            new ColumnSet(true));

但是,如果我不知道它是什么类型的记录,我该如何检索它并识别它的类型?

像这样的东西:

        var myEntity = organizationService.Retrieve(
            "????",
            myEntityGuid,
            new ColumnSet(true));
c# .net visual-studio dynamics-crm-2011 dynamics-crm
4个回答
1
投票

您无法单独通过GUID进行搜索。您需要GUID和实体逻辑名称。

每当使用代码,c#或javascript检索实体引用时,该对象将包含实体逻辑名称

更新以尝试查找具有给定ID的帐户:

Guid Id = // Your GUID
IOrganizationService serviceProxy = // Create your serivce proxy 
var accountQuery = new QueryExpression
                   {
                       EntityName = "account",
                       TopCount = 1,
                       Criteria =
                       { 
                           Conditions = 
                           { 
                               new ConditionExpression("accountid", ConditionOperator.Equal, Id) 
                           }
                       }
                    } 
var response = serviceProxy.RerieveMultiple(accountQuery);
if(null == response.Entities.FirstOrDefault())
{
    //No Account Record Found 
}

2
投票

如果您从Nuget引用DLaB.Xrm,您可以这样写:

bool isAccount = service.GetEntitiesById<Account>(customerId).Count == 1;

如果你想真正获得实际值,你可以这样做。

var customerId = System.Guid.NewGuid();
var entity = service.GetEntitiesById<Account>(customerId).FirstOrDefault() ??
             service.GetEntitiesById<Contact>(customerId).FirstOrDefault() as Entity;

if (entity != null)
{
    var account = entity as Account; // will be null if the Guid was for a contact
    var contact = entity as Contact; // will be null if the Guid was for an account
}

1
投票

如果您只需要区分帐户和联系人,并希望确保记录确实存在,您还可以使用他们的CustomerAddress,LEFT OUTER JOINing,帐户和联系人:

var query = new QueryExpression
{
    EntityName = "customeraddress",
    ColumnSet = new ColumnSet("customeraddressid"),
    TopCount = 1,
    Criteria = new FilterExpression
    {
        Conditions =
        {
            // limit to Address 1
            new ConditionExpression("addressnumber", ConditionOperator.Equal, 1),
            // filter by "anonymous" GUID
            new ConditionExpression("parentid", ConditionOperator.Equal, myEntityGuid),
        },
    },
    LinkEntities =
    {
        new LinkEntity
        {
            EntityAlias = "acc",
            Columns = new ColumnSet("name"),
            LinkFromEntityName = "customeraddress",
            LinkFromAttributeName = "parentid",
            LinkToAttributeName = "accountid",
            LinkToEntityName = "account",
            JoinOperator = JoinOperator.LeftOuter
        },
        new LinkEntity
        {
            EntityAlias = "con",
            Columns = new ColumnSet("fullname"),
            LinkFromEntityName = "customeraddress",
            LinkFromAttributeName = "parentid",
            LinkToAttributeName = "contactid",
            LinkToEntityName = "contact",
            JoinOperator = JoinOperator.LeftOuter
        },
    },
};

...允许您一次性检索帐户或联系人:

var customer = service.RetrieveMultiple(query).Entities.FirstOrDefault();

...但需要通过AliasedValues访问他们的字段:

string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;

...这可以使阅读很多属性有点混乱。


1
投票

我知道这是一个古老的问题,但我想我会添加一些东西,以防将来有人因为类似我的问题而弄明白。在这种情况下,它可能与OP要求的不同。

我必须确定一个实体是一个还是另一个类型,所以我可以使用一种方法而不必为每个实体编写独立的方法。开始:

var reference = new EntityReference
{
    Id = Guid.NewGuid(),
    LogicalName = "account" //Or some other logical name - "contact" or so.
}

通过将其传递给以下方法,可以识别类型。

public void IdentifyType(EntityReference reference)
{
    switch(reference.LogicalName)
    {
        case Account.EntityLogicalName:
            //Do something if it's an account.
            Console.WriteLine($"The entity is of type {nameof(Account.EntityLogicalName)}."
        case Contact.EntityLogicalName:
            //Do something if it's a contact.
            Console.WriteLine($"The entity is of type {nameof(Contact.EntityLogicalName)}."
        default:
            //Do something if neither of above returns true.
            Console.WriteLine($"The entity is not of type {nameof(Account.EntityLogicalName)} or {nameof(Contact.EntityLogicalName)}."
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.