如 使用 FetchXml 的查询数据中所述,使用 实体元素选择表来启动查询。
使用 属性元素 选择要随查询一起返回的列。 例如:
<fetch>
<entity name='account'>
<attribute name='accountclassificationcode' />
<attribute name='createdby' />
<attribute name='createdon' />
<attribute name='name' />
</entity>
</fetch>
此查询返回 AccountClassificationCode、CreatedBy、CreatedOn 和 Name 列,这些列来自 Account 表中的前 5,000 行记录。 如果需要的行数超过此限制,或者想要按较小批次遍历数据,请了解如何使用 FetchXml 对结果进行分页。
对于要返回的每个属性,请添加一个属性元素,并将name属性值设置为该列的LogicalName。
使用 属性元素 选择查询 实体 的列,以及使用 链接实体元素联接的任何表。 了解如何使用 FetchXml 联接表。
Important
我们强烈建议不要返回表中的所有列。 返回所有列会使应用程序运行速度变慢,并可能导致超时错误。 应指定随数据一起检索的最小列数。 如果未指定列或使用 all-attributes 元素,则返回所有列的数据。
格式化值
返回的键入的数据可能不适合在应用程序中显示。 格式化值是随请求一起返回的字符串值,可在应用程序中显示。
让我们先看看不使用格式化值的结果。 这些函数使用 ConsoleTables NuGet 包 来在控制台应用程序中显示表格。
此方法 SimpleOutput 仅访问 Entity.Attributes 集合中的值。
/// <summary>
/// Output the entity attribute values
/// </summary>
/// <param name="service">The authenticated IOrganizationService instance</param>
static void SimpleOutput(IOrganizationService service) {
string fetchXml = @"<fetch>
<entity name='account'>
<attribute name='accountclassificationcode' />
<attribute name='createdby' />
<attribute name='createdon' />
<attribute name='name' />
</entity>
</fetch>";
FetchExpression fetchExpression = new(fetchXml);
//Retrieve the data
EntityCollection entityCollection = service.RetrieveMultiple(query: fetchExpression);
var table = new ConsoleTables.ConsoleTable("classificationcode", "createdby", "createdon", "name");
foreach (var entity in entityCollection.Entities ) {
var accountclassificationcode = entity.GetAttributeValue<OptionSetValue>("accountclassificationcode").Value;
var createdby = entity.GetAttributeValue<EntityReference>("createdby").Name;
var createdon = entity.GetAttributeValue<DateTime>("createdon");
var name = entity.GetAttributeValue<string>("name");
table.AddRow(accountclassificationcode, createdby, createdon, name);
}
table.Write();
}
输出:
----------------------------------------------------------------------------------------------
| classificationcode | createdby | createdon | name |
----------------------------------------------------------------------------------------------
| 1 | FirstName LastName | 8/13/2023 10:30:08 PM | Fourth Coffee (sample) |
----------------------------------------------------------------------------------------------
| 1 | FirstName LastName | 8/13/2023 10:30:10 PM | Litware, Inc. (sample) |
----------------------------------------------------------------------------------------------
| 1 | FirstName LastName | 8/13/2023 10:30:10 PM | Adventure Works (sample)|
----------------------------------------------------------------------------------------------
这些值可能不是需要在应用程序中显示的用户友好值。
-
accountclassificationcode选项列返回整数值。 - 对 的 SDK 引用必须使用
createdby - Web API 返回
_createdby_valuecreatedby,其包含 查阅列的 GUID 值。
若要获取所需的用户友好值,需要访问 Dataverse 可以返回的格式化值。
获取这些值的方式取决于你使用的是适用于 .NET 的 SDK 还是 Web API。
OutputFetchRequest
FetchXml 示例代码中所述的示例方法使用 Entity.FormattedValues 集合中的数据,因此查询的结果如下所示:
--------------------------------------------------------------------------------------------------
| accountclassificationcode | createdby | createdon | name |
--------------------------------------------------------------------------------------------------
| Default Value | FirstName LastName | 8/13/2023 10:30 PM | Fourth Coffee (sample) |
--------------------------------------------------------------------------------------------------
| Default Value | FirstName LastName | 8/13/2023 10:30 PM | Litware, Inc. (sample) |
--------------------------------------------------------------------------------------------------
| Default Value | FirstName LastName | 8/13/2023 10:30 PM | Adventure Works (sample)|
--------------------------------------------------------------------------------------------------
此方法 GetRowValues 从 Entity.FormattedValues 中提取记录的字符串值列表(如果可用)。
/// <summary>
/// Returns the values of a row as strings
/// </summary>
/// <param name="columns">The names of the columns</param>
/// <param name="entity">The entity with the data</param>
/// <returns></returns>
static List<string> GetRowValues(List<string> columns, Entity entity)
{
List<string> values = new();
columns.ForEach(column =>
{
if (entity.Attributes.ContainsKey(column))
{
// Use the formatted value if it available
if (entity.FormattedValues.ContainsKey(column) &&
!string.IsNullOrWhiteSpace(entity.FormattedValues[column]))
{
values.Add($"{entity.FormattedValues[column]}");
}
else
{
// When an alias is used, the Aliased value must be converted
if (entity.Attributes[column] is AliasedValue aliasedValue)
{
// When an EntityReference doesn't have a Name, show the Id
if (aliasedValue.Value is EntityReference lookup &&
string.IsNullOrWhiteSpace(lookup.Name))
{
values.Add($"{lookup.Id:B}");
}
else
{
values.Add($"{aliasedValue.Value}");
}
}
else
{
// Use the simple attribute value
values.Add($"{entity.Attributes[column]}");
}
}
}
// Null values are not in the Attributes collection
else
{
values.Add("NULL");
}
});
return values;
}
详细了解格式化值:
列别名
列别名通常用于 聚合操作,但它们也适用于简单的选择操作,因此我们可以在此处介绍它们。
使用 属性alias 属性为返回的结果指定唯一的列名称。
返回的每一列都必须具有唯一的名称。 默认情况下,为查询的表返回的列名是列 LogicalName 值。 每个表的所有列逻辑名称都是唯一的,因此该集内没有任何重复的名称。
使用链接实体元素联接表时,默认列名称遵循以下命名约定: {Linked table LogicalName}.{Column LogicalName} 这可以防止出现重复的列名。 可以使用唯一的别名来覆盖此项。 还可以为表示联接表的alias设置link-entity值。
使用列别名时看到的行为取决于你使用的是适用于 .NET 的 SDK 还是 Web API。
此方法 SimpleAliasOutput 使用别名,而不是列的逻辑名称。 因此,结果将以 AliasedValue 的形式返回。 若要访问 OptionSetValue 或 EntityReference 等复杂类型的值,必须强制转换该值。
此方法使用 ConsoleTables NuGet 包 。
/// <summary>
/// Output the entity attribute values with aliases
/// </summary>
/// <param name="service">The authenticated IOrganizaitonService instance</param>
static void SimpleAliasOutput(IOrganizationService service)
{
string fetchXml = @"<fetch top='3'>
<entity name='account'>
<attribute name='accountclassificationcode' alias='code' />
<attribute name='createdby' alias='whocreated' />
<attribute name='createdon' alias='whencreated' />
<attribute name='name' alias='companyname' />
</entity>
</fetch>";
FetchExpression fetchExpression = new(fetchXml);
//Retrieve the data
EntityCollection entityCollection = service.RetrieveMultiple(query: fetchExpression);
var table = new ConsoleTables.ConsoleTable("code", "whocreated", "whencreated", "companyname");
foreach (var entity in entityCollection.Entities)
{
var code = ((OptionSetValue)entity.GetAttributeValue<AliasedValue>("code").Value).Value;
var whocreated = ((EntityReference)entity.GetAttributeValue<AliasedValue>("whocreated").Value).Name;
var whencreated = entity.GetAttributeValue<AliasedValue>("whencreated").Value;
var companyname = entity.GetAttributeValue<AliasedValue>("companyname").Value;
table.AddRow(code, whocreated, createdon, companyname);
}
table.Write();
}
Output:
----------------------------------------------------------------------------------
| code | whocreated | whencreated | companyname |
----------------------------------------------------------------------------------
| 1 | FirstName LastName | 8/13/2023 10:30:08 PM | Fourth Coffee (sample) |
----------------------------------------------------------------------------------
| 1 | FirstName LastName | 8/13/2023 10:30:10 PM | Litware, Inc. (sample) |
----------------------------------------------------------------------------------
| 1 | FirstName LastName | 8/13/2023 10:30:10 PM | Adventure Works (sample) |
----------------------------------------------------------------------------------
Note
AliasedValue 类有两个属性,用于告知原始 EntityLogicalName 和 AttributeLogicalName(如果需要)。
后续步骤
了解如何连接表。