locked
How do I compute the sum of several "Money" type properties (some may contain NULL values) in a computed property of an entity in LIghswitch using C# RRS feed

  • Question

  • I am developing a Lightswitch Application and in one of the Entities I have several currency type properties and I would like to add them all up in one calculated property using C#. For instance:

    I have 3 columns Amount1                 Amount2                 Amount3 And would like the total sum of the 3 fields.

    With result = Amount1 + Amount2 + Amount3

    The will add all the column as long as there is data but if Amount2 is NULL then no result is written to the computed property.


    Saturday, January 10, 2015 6:41 PM

Answers

  • Hi,

    partial void MF2016_Validate() is for doing any validation on your data, not to assign a value to a property.

    something like this (the error message will display on the screen),

    partial void MF2016_Validate(EntityValidationBuilder results) { if (!MF2016.HasValue) { results.AddPropertyError("Please enter a value in MF2016"); } }

    the assign is done on TotalDue_Computed(ref decimal? result)

    result = MF2016.HasValue ? (decimal)MF2016 : 0 + ....;



    • Marked as answer by Angie Xu Monday, January 19, 2015 1:45 AM
    Monday, January 12, 2015 11:54 PM

All replies

  • You can check for null first, if null sum 0

    result = (Amount1.HasValue ? (decimal)Amount1 : 0)

    + (Amount2.HasValue ? (decimal)Amount2 : 0)

    + (Amount3.HasValue ? (decimal)Amount3 : 0)

    Daniel

    Monday, January 12, 2015 2:31 AM
  • Daniel,

    Your solution looks like it is exactly what I need but when put it in I get the message "The name 'result' does not exist in the current context". I know that "result" needs to be declare for decimal but I am not sure where or how.  Thank you for you reply. Your help is appreciated.

    Monday, January 12, 2015 1:09 PM
  • Here is the actual code.

    using

    System;


    using

    System.Collections.Generic;


    using

    System.Linq;


    using

    System.Text;


    using

    Microsoft.LightSwitch;


    namespace

    LightSwitchApplication

    {

       

    publicpartialclassTransaction


        {

           

    partialvoidTotalDue_Compute(refdecimal? result)

            {

               

    // Set result to the desired field value


                result = StandardServiceFee + TransferFee + EstoppleFee + StateSurcharge + AttorneySurcharge + CountyRecordingSurcharge + OutOfCountryFee + ROFR + RCITransferFee + OtherFee + MF2015 + MF2016;

            }

           

    partialvoidMF2016_Validate(EntityValidationResultsBuilderresults)

            {

               

    // results.AddPropertyError("<Error-Message>");


                result = (MF2016.HasValue ? (

    decimal)MF2016 : 0);

            }

        }

    }

    Monday, January 12, 2015 1:14 PM
  • Hi,

    partial void MF2016_Validate() is for doing any validation on your data, not to assign a value to a property.

    something like this (the error message will display on the screen),

    partial void MF2016_Validate(EntityValidationBuilder results) { if (!MF2016.HasValue) { results.AddPropertyError("Please enter a value in MF2016"); } }

    the assign is done on TotalDue_Computed(ref decimal? result)

    result = MF2016.HasValue ? (decimal)MF2016 : 0 + ....;



    • Marked as answer by Angie Xu Monday, January 19, 2015 1:45 AM
    Monday, January 12, 2015 11:54 PM