Konrad Viltersten
Konrad Viltersten

Reputation: 39058

Find out the names of all the attributes in an entity returned from CRM Dynamics

I got myself into the server and I have retrieved (hopefully) the correct set of data. Then, I tried to list all the companies by the following code.

EntityCollection result = proxy.RetrieveMultiple(expression);
foreach (var entity in result.Entities)
{
  String output = String.Empty;
  if (entity.Attributes.Contains("account"))
    output = entity.Attributes["account"].ToString();
}

However, it'd be nice to run an inner loop that goes through all the available attributes in result.Entities. Should I use entity.Attributes.Keys or is there a better method?

Upvotes: 8

Views: 23851

Answers (2)

Nick Haslam
Nick Haslam

Reputation: 1510

This carries out the task using a Lambda expression.

EntityCollection result = proxy.RetrieveMultiple(expression);
foreach (var entity in result.Entities)
{
    var vsHeaders = entity.Attributes.Select(kvp => string.Format("{0}", kvp.Key));
    string sHeaders = string.Join(",", vsHeaders);
}

Upvotes: 0

James Wood
James Wood

Reputation: 17552

I think this should do the trick.

foreach (Entity entity in result.Entities)
{
    foreach (KeyValuePair<String, Object> attribute in entity.Attributes)
    {
        Console.WriteLine(attribute.Key + ": " + attribute.Value);
    }
} 

Upvotes: 14

Related Questions