从CDS实体

时间:2018-03-06 03:50:21

标签: c# common-data-service

在Common Data Services SDK Microsoft.CommonDataService中,我可以使用此代码从数据库中选择实体

// Specify the generic object to retrieve
var genericEntitySet = client.GetRelationalEntitySet(
    new EntitySetReference("Microsoft.CommonDataService.CommonEntities",
    AdvEntity.EntityName,
    Microsoft.CommonDataService.Version.Create("1.0.0")));

// Specify the required fields
var query = genericEntitySet.CreateQueryBuilder()
    .Project(pc => 
        pc
            .SelectField(f => f["PrimaryId"])
            .SelectField(f => f["Project_Title"])
            .SelectField(f => f["CreatedByUser"])
            .SelectField(f => f["CreatedOnDateTime"])
            .SelectField(f => f["LastModifiedByUser"])
            .SelectField(f => f["LastModifiedDateTime"])
            .SelectField(f => f["Opportuinity"])
            .SelectField(f => f["BNB"])
            .SelectField(f => f["SP_URL"])
            .SelectField(f => f["ProjectMgr"])
            .SelectField(f => f["WorkTeam"])
            .SelectField(f => f["BnB_ID"])
            .SelectField(f => f["Custodian"])
            .SelectField(f => f["Stage"])
            .SelectField(f => f["Opp_Status"])
            .SelectField(f => f["Confidential"])
            .SelectField(f => f["CRMT_Num"])
            .SelectField(f => f["Proj_Num"])
            .SelectField(f => f["Sector"])
            .SelectField(f => f["Service"])
            .SelectField(f => f["Archive"])
        );

var selectExecutor = client.CreateRelationalBatchExecuter(
    RelationalBatchExecutionMode.Transactional);

await selectExecutor
    .Query(query, out OperationResult<IReadOnlyList<RelationalEntity>> queryResult)
    .ExecuteAsync();

但是,为我可能需要检索的每种类型的对象选择所有这些字段似乎很麻烦。

有没有办法告诉代码获取所有属性

The documentation表示

  

请注意,出于性能原因,没有选项可以自动选择所有字段。

此SDK已预览,因此没有太多关于它的信息,但也许有人知道这个技巧?

2 个答案:

答案 0 :(得分:0)

简要历史记录:您正在谈论的CDS 1.0是基于Dynamics AX构建的旧版本,现在已淘汰。

基于Dynamics CRM(又称为Dynamics客户参与(CE),又称为Dynamics 365)的全新CDS 2.0,称为应用程序CDS。该应用程序CDS具有诸如QueryExpression和FetchXML之类的查询概念,您可以在其中提及所有属性(ColumnSet = new ColumnSet(true))以像SQL(select * from table)一样进行检索

var request = new RetrieveRequest()
{
  ColumnSet = new ColumnSet(true),
  RelatedEntitiesQuery = relationshipQueryCollection,
  Target = new EntityReference("account", accountid)
};

Reference

从性能角度来看,仍然不建议这样做。

答案 1 :(得分:-1)

根据documentation ...

static async Task SimpleSelectAsync(Client client)
{
    var queryBuilder = client.GetRelationalEntitySet<ProductCategory>()
        .CreateQueryBuilder();

    var query = queryBuilder
        .Where(pc => pc.Name == "Electronics")
        .OrderByAscending(pc => new object[] { pc.CategoryId })
        .Project( /* REMOVE THIS STUFF?.. pc => pc.SelectField(f => f.CategoryId)
            .SelectField(f => f.Name)
            .SelectField(f => f.Description) */ );

    // Execute the query:
    OperationResult<IReadOnlyList<ProductCategory>> queryResult = null;
    var executor = client.CreateRelationalBatchExecuter(
        RelationalBatchExecutionMode.Transactional)
        .Query(query, out queryResult);

    await executor.ExecuteAsync();

    foreach (var pc in queryResult.Result)
    {
        Console.WriteLine(pc.Name);
    }
}