在使用FetchXml查询链接实体时,如果不希望在结果中使用别名,可以使用以下代码示例来解决:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
// 创建一个查询表达式对象
QueryExpression query = new QueryExpression("account");
// 设置查询列
query.ColumnSet = new ColumnSet("name");
// 添加与链接实体的关联
LinkEntity linkEntity = new LinkEntity()
{
LinkFromEntityName = "account",
LinkFromAttributeName = "accountid",
LinkToEntityName = "contact",
LinkToAttributeName = "parentcustomerid",
JoinOperator = JoinOperator.Inner
};
// 设置关联实体的别名
linkEntity.EntityAlias = "contact";
// 添加关联实体的列
linkEntity.Columns = new ColumnSet("fullname");
// 将关联实体添加到查询表达式中
query.LinkEntities.Add(linkEntity);
// 执行查询
EntityCollection results = service.RetrieveMultiple(query);
// 遍历结果集
foreach (Entity entity in results.Entities)
{
// 获取主实体的列值
string accountName = entity.GetAttributeValue("name");
// 获取关联实体的列值
string contactName = entity.GetAttributeValue("contact.fullname").Value.ToString();
Console.WriteLine("Account Name: " + accountName);
Console.WriteLine("Contact Name: " + contactName);
}
在上述代码示例中,我们使用了AliasedValue
类来获取链接实体的别名结果。通过使用AliasedValue
类,我们可以避免在结果中使用别名。
请注意,service
变量是一个IOrganizationService
接口的实例,你需要根据你的实际情况进行实例化。