Thursday, 14 July 2016

Different ways of doing converting list to custom arrays


List<EntityReference> list = new List<EntityReference> { new EntityReference("contact", Guid.NewGuid()), new EntityReference("contact", Guid.NewGuid()) };

 
Option 1


Entity[] listArray = new Entity[toList.Count];

 

for (int i = 0; i < toList.Count; i++)

{

Entity toParty = new Entity("activityparty");

toParty["partyid"] = toList[i];

listArray[i] = toParty;

}


Option 2
 
Entity[] listArray = new Entity[toList.Count];


list.ForEach(delegate(EntityReference item)

{

Entity ccParty = new Entity("activityparty");

       ccParty["partyid"] = item;

       listArray.SetValue(ccParty, listArray.Count());

});


Option 3
 

Entity[] listArray = ccList.ConvertAll<Entity>(delegate(EntityReference item) { return new Entity { LogicalName = "activityparty", Attributes = { { "partyid", item }   } }; }).ToArray();

No comments:

Post a Comment