I have the below requirement to be implemented in a plugin code on an Entity say 'Entity A'-
Below is the data in 'Entity A'
Record 1 with field values
- Price = 100
- Quantity = 4
Record 2 with field values
- Price = 200
- Quantity = 2
I need to do 2 things
- Add the values of the fields and update it in a new record
- Store the Addition Formula in a different config entity
Example shown below -
Record 3
Price
Price Value = 300 Formula Value = 100 + 200Quantity Quantity Value = 6 Formula Value = 4 + 2
Entity A has a button named "Perform Addition" and once clicked this will trigger the plugin code.
Below is the code that i have tried -
AttributeList is the list of fields i need to perform sum on. All fields are decimal
Entity EntityA = new EntityA();
EntityA.Id = new Guid({"Guid String"});
var sourceEntityDataList = service.RetrieveMultiple(new FetchExpression(fetchXml)).Entities;
foreach (var value in AttributeList)
{
EntityA[value]= sourceEntityDataList.Sum(e => e.Contains(value) ? e.GetAttributeValue<Decimal>(value) : 0);
}
service.Update(EntityA);
I would like to know if there is a way through linq I can store the formula without looping? and if not how can I achieve this?
Any help would be appreciated.