I'm sure this is really simple but I'm not really a developer. I'm querying CRM using FetchXML and RetrieveMultiple. The querys runs against a custom entity and returns a total grouped by account (which is a lookup in the custom entity). The FetchXML looks like this:
string fetchquery = @"<fetch distinct='false' mapping='logical' aggregate='true'><entity name='new_customentity'><attribute name='new_usercount' alias='UsercountSum' aggregate='Sum'/><attribute name='new_account' groupby='true' alias='Account'/></entity></fetch>";
I retrieve the results using:
EntityCollection fetchquery_result = _service.RetrieveMultiple(new FetchExpression(fetchquery));
Now, I want to loop through the results and use the total user count for each account to update a field on the account. I can get at the user count using:
foreach (var c in fetchquery_result.Entities) { int usercount = (int)((AliasedValue)c.Attributes["UsercountSum"]).Value; }
But I can't work out how to reference the guid of the account record to use in an Update call. I've tried:
Guid thisaccountid = (Guid)((AliasedValue)c.Attributes["Account"]).Value;
but that returns an error, Specified cast is not valid. When casting from a number, the value must be a number less than infinity.
Any insight here would be highly appreciated.