lundi 22 juillet 2019

Repeated methods call different properties of a model

Is it possible to simplify this code? I am transforming data from a list of values into the same Object. In each method the property being summed changes.

Properties follow the same naming convention every time (paios or pa) followed by (InUse or SuportInUse). I know in JavaScript I could generate the strings and eval the string, but as far as I know there is nothing similar in C#.

public class CompanyLicValues
{
    public int Paios;
    public int Papc;
    public int Paplus;
    public int Eis;
    public int Pd;
    public int Dropoff;

    public CompanyLicValues SumInUse(List<vwCompany> companies)
    {
        Paios = companies.Sum(x => x.paiosInUse); //<<<The property being summed changes
        Papc = companies.Sum(x => x.papcInUse); //<<<The property being summed changes
        ....
        return this;
    }

    public CompanyLicValues SumSupportUsed(List<vwCompany> companies)
    {
        Paios = companies.Sum(x => x.paiosSupportInUse); //<<<The property being summed changes
        Papc = companies.Sum(x => x.papcSupportInUse); //<<<The property being summed changes
        ...
        return this;
    }
 }

Aucun commentaire:

Enregistrer un commentaire