0

i have code like below:

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);

Trough this code. I am able to get data from microsoft dynamic entity. Now, i want to get the data which have biggest id. If in SQL Query, it would be looks something like this : "Select top 1 my_id from Account order by my_id desc". How can i do that on queryexpression ? Thanks

Fikri Hailal
  • 123
  • 2
  • 4
  • 16

1 Answers1

2

You can add the order by using this:

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

and then getting the first element retrieved.

var entityCollection = service.RetrieveMultiple(query);
if(entityCollection.Entities.Count<1)
{
    //perform some logic
}
MaPi
  • 1,601
  • 3
  • 31
  • 64
  • Thanks marco, can i ask you one more question? how can i check if queryexpression is contain a data or not. I was figured to use "TotalRecordCount". Is that the true ways ? – Fikri Hailal Nov 04 '15 at 07:54
  • After the execute of the retrieve, assuming you store your entities in a variable called retrievedEntities you can check the size of the collection with retrievedEntities.Entities.Count – MaPi Nov 04 '15 at 07:56
  • i dont get it: `if (colect.TotalRecordCount < 1) { entity["new_nomororder"] = "ODR-C7HI-001"; } else { string str = string.Empty; foreach (Entity e in colect.Entities) { if (e.Contains("new_nomororder")) { str = str + e.Attributes["new_nomororder"].ToString(); } }` i want to set field with value if queryexpression dosn;t contain data – Fikri Hailal Nov 04 '15 at 07:58
  • Modified the answer after your comment – MaPi Nov 04 '15 at 08:03