Search code examples
c#pluginsdynamics-crmmicrosoft-dynamicsdynamics-crm-online

Microsoft Dynamics CRM - Plug-in C# - add record to entity collection


Plug-In, C#, Microsoft Dynamics CRM Online

I want to add a record (let's call it "sampleRecord") to an Entity Collection (let's call it "sampleCollection"), but somehow I can't make it work. I found this solution on the internet, but when I check the total record count via ITracingService, it's still 0.

my solution so far:

EntityCollection sampleCollection = new EntityCollection();
sampleCollection.Entities.Add(sampleRecord);

and that's how I checked the total record count:

tracingService.Trace("total record count: " + sampleCollection.TotalRecordCount.ToString());

Thanks in advance for any help!


Solution

  • You're looking at the wrong value. TotalRecordCount is a result from the query execution (not the count of .Entities. If you use .Entities.Count() you should get the correct value, as shown here:

    var entityCollection = new EntityCollection();
    
    Console.WriteLine(entityCollection.Entities.Count()); // 0
    
    entityCollection.Entities.Add(new Entity());
    
    Console.WriteLine(entityCollection.Entities.Count()); // 1