Select 实体中数字字段的最大数量 (Dynamics CRM 2016)

Select Maximum number from a numeric field in an entity (Dynamics CRM 2016)

从实体的数字字段中检索最大值的最佳方法是什么? SQL 服务器中的类似内容:Select MAX(NumbericFieldName) From TableName

我试过这个:

var documentno = XrmContext.CreateQuery("nychro_traportaldocumentupload").Max(c => c.GetAttributeValue<Int32?>("nychro_portaldocumentreviewid"));

但是我收到错误 "MAX is not supported"

解决此问题的最佳方法是什么?

您必须使用 fetchxml 查询并执行 FetchExpression 来获取结果。

<fetch distinct='false' mapping='logical' aggregate='true'> 
    <entity name='nychro_traportaldocumentupload'> 
       <attribute name='nychro_portaldocumentreviewid' alias='nychro_portaldocumentreviewid_max' aggregate='max' /> 
    </entity> 
</fetch>

下面的linq代码,应该可以完成你的要求:

var documentno = (for a in XrmContext.CreateQuery("nychro_traportaldocumentupload")
                 orderby a.nychro_portaldocumentreviewid descending
                 select a).FirstOrDefault()