如何识别实体类型?
How to identify the type of entity?
如果我有一个记录的 GUID,但我不知道它是一个帐户还是一个联系人,我如何检索该记录并识别它的类型?
例如,我可以像这样检索 指定的 类型:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
但是,如果我不知道它是什么类型的记录,我如何检索它并识别它的类型?
像这样:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
您不能仅通过 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
}
如果你从 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
}
如果您只需要区分 Account 和 Contact 并想确保记录确实存在,您也可以使用他们的 CustomerAddress,LEFT OUTER JOIN
两者兼顾,Account 和 Contact:
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();
...但需要通过 AliasedValue
s:
访问他们的字段
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
...这会使阅读很多属性有点混乱。
我知道这是一个老问题,但我想我会添加一些内容,以防将来有人遇到与我类似的问题。在这种情况下,它可能与 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)}."
}
}
如果我有一个记录的 GUID,但我不知道它是一个帐户还是一个联系人,我如何检索该记录并识别它的类型?
例如,我可以像这样检索 指定的 类型:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
但是,如果我不知道它是什么类型的记录,我如何检索它并识别它的类型?
像这样:
var myEntity = organizationService.Retrieve(
"????",
myEntityGuid,
new ColumnSet(true));
您不能仅通过 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
}
如果你从 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
}
如果您只需要区分 Account 和 Contact 并想确保记录确实存在,您也可以使用他们的 CustomerAddress,LEFT OUTER JOIN
两者兼顾,Account 和 Contact:
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();
...但需要通过 AliasedValue
s:
string customername = (customer.GetAttributeValue<AliasedValue>("acc.name") ?? customer.GetAttributeValue<AliasedValue>("con.fullname") ?? new AliasedValue("whatever", "whatever", null)).Value as string;
...这会使阅读很多属性有点混乱。
我知道这是一个老问题,但我想我会添加一些内容,以防将来有人遇到与我类似的问题。在这种情况下,它可能与 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)}."
}
}