Microsoft CRM QueryExpression in C#

本文关键字:in QueryExpression CRM Microsoft | 更新日期: 2023-09-27 18:20:25

我有如下代码:

QueryExpression query = new QueryExpression();
            query.EntityName = "new_callistyorder"; 
            ColumnSet col = new ColumnSet("new_nomororder","new_customer");
            query.ColumnSet = col;
            EntityCollection colect = service.RetrieveMultiple(query);
            string str = string.Empty;
            foreach (Entity e in colect.Entities)
            {
                if(e.Contains("new_nomororder")){
                str = str + e.Attributes["new_nomororder"].ToString();
                }
            }
            throw new InvalidPluginExecutionException(str);

通过这个代码。我能够从微软的动态实体中获取数据。现在,我想得到id最大的数据。如果在SQL Query中,它看起来像这样:"通过my_id desc从Account order中选择前1个my_id"。如何对queryexpression执行此操作?感谢

Microsoft CRM QueryExpression in C#

您可以使用以下命令添加订单:

query.AddOrder("my_id", OrderType.Descending);

然后获取第一个元素。

var entityCollection = service.RetrieveMultiple(query);
if(entityCollection.Entities.Count<1)
{
    //perform some logic
}