codeeffects

How to compare a field in a collection to a field in the parent object


Using the Angular Demo from here, I added a collection of HospitalVisit objects and I wanted to answer the question, "has patient visited an out of state hospital".

Ask you can see I can only compare the HospitalVisit.State field to a string, another field on the same current object (or a dynamic data source), but it can not reference the parent object. How would I do this?

Something like

var p = new Patient();
        var homeSate = p.State;
        var hasBeenOutOfState = p.HospitalVisits.Any(h=> h.State != homeSate);

enter image description here


Solution

  • You cannot get a reference to your main source from sub-sources. Instead, you should declare an in-rule method and get the desired value that way. For instance, you could declare the following method in your Patient type:

    [ Method( DisplayName = "Out of State" ) ]
    public bool HasOutOfStateVisits(List<HospitalVisit> visits)
    {
       return visits != null && visits.Any(v => v.State != this.State);
    }
    

    And then use it in your rules like this:

    Check if State has any value and Out of State(HospitalVisits) equals to True