【问题标题】:How to assign a score system to a large if-else statement ASP.Net如何将评分系统分配给大型 if-else 语句 ASP.Net
【发布时间】:2019-08-08 21:41:24
【问题描述】:

我正在开展一个项目,该项目由一个医疗患者记录系统组成,该系统在其中存储大量患者信息。当医生/护士添加新患者时,他们必须查看大量病史条件等,例如患者是否患有癌症、是否吸烟、是否肥胖等。每个变量都是布尔值并且是声明为是或否,值为 1 或 0。

需要患者输入表上的所有变量,因为它们是更大风险评估计算器的一部分。这些条件中的每一个都有特定的值,例如,如果患者患有癌症,则选择是。这将是为每位患者整体添加的评分系统的 +1 分。某些变量(例如肥胖)的答案可能超过 1 个,因此它们会得到 0/1/2 分。

所有这些变量都放在一个 patient.cs 类中,BMI 计算器等功能也是如此。我将在哪里以及如何为我的每个变量分配一个分数? (有超过 15 个不同的变量,所以它必须是一个很大的 if-else 语句)

我们将不胜感激。

-----------[编辑]---------- ----------------

The code below is my patient.cs class as of now

 public class Patient
    {
        //The patients ID
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int patientId { get; set; }

        [ForeignKey("ApplicationUser")]
        public string Id { get; set; }
        public virtual ApplicationUser ApplicationUser { get; set; }

        public string firstName { get; set; }

        public string middleName { get; set; }

        public string lastName { get; set; }

        public string gender { get; set; }

        public DateTime birthdate { get; set; }

        public decimal weight { get; set; }

        public decimal height { get; set; }


        /*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Pre-existing risk factors
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~          
         */

        //Parity is the amount of pregnancies had
        public int parity { get; set; }
        // single, married, seperated or divorced
        public string civilStatus { get; set; }
        //  yes = 4 | no = 0
        public bool previousVTE { get; set; }
        // Provoked by major surgery?  yes = 3 | no = 0
        public bool surgeryVTE { get; set; }
        //  yes = 3 | no = 0
        public bool highRiskThrombophilia { get; set; }
        //  yes =  | no = 
        public bool cancer { get; set; }
        //  yes = 3 | no = 0
        public bool heartFailure { get; set; }
        //  yes = 3 | no = 0
        public bool activeSystemicLupusErythematosus { get; set; }
        //  yes = 3 | no = 0
        public bool inflammatoryPolyarthropathy { get; set; }
        //  yes = 3 | no = 0
        public bool inflammatoryBowelDisease { get; set; }
        //  yes = 3 | no = 0
        public bool nephroticSyndrome { get; set; }
        //  yes = 3 | no = 0
        public bool typeIDiabetesMellitusWithNephropathy { get; set; }
        //  yes = 3 | no = 0
        public bool sickleCellDisease { get; set; }
        //  yes = 3 | no = 0
        public bool currentInratvenousDrugUser { get; set; }
        // Family history of unprovoked / estrogen-related VTE in first-degree relative? yes = 1 | no = 0
        public bool familyHistoryVTEFirstDegreeRelative { get; set; }
        //  yes = 1 | no = 0
        public bool lowRiskThrombophilia { get; set; }
        //  yes = 1 | no = 0
        public bool smoker { get; set; }
        //  yes = 1 | no = 0
        public bool grossVaricoseVeins { get; set; }

        //  0 = no, 1=>30 2=>40
        public string obesity { get; set; }


        /*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Obstetric risk factors
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     
         */

        //  yes = 1 | no = 0
        public bool preEclampsiaInCurrentPregnancy { get; set; }
        // ART/IVF (antenatal only) yes = 1 | no = 0
        public bool ARTorIVF { get; set; }
        //  yes = 1 | no = 0
        public bool multiplePregnancy { get; set; }
        //  yes = 2 | no = 0
        public bool caesareanSectionInLabour { get; set; }
        //  yes = 1 | no = 0
        public bool electiveCaesareanSection { get; set; }
        // Mid-cavity or rotational operative delivery yes = 1 | no = 0
        public bool operativeDelivery { get; set; }
        // Prolonged labour (> 24 hours) yes = 1 | no = 0
        public bool prolongedLabour { get; set; }
        // PPH (> 1 litre or transfusion) yes = 1 | no = 0
        public bool PPH { get; set; }
        // Preterm birth < 37+0 weeks in current pregnancy yes = 1 | no = 0
        public bool pretermBirth { get; set; }
        // Stillbirth in current pregnancy yes = 1 | no = 0
        public bool stillBirth { get; set; }

        /*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Transient risk factors
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     
         */

        // Any surgical procedure in pregnancy or puerperium except immediate repair of the
        // perineum, e.g.appendicectomy, postpartum sterilisation yes = 3 | no = 0
        public bool surgicalProcedure { get; set; }
        // yes = 3 | no = 0
        public bool Hyperemesis { get; set; }
        // yes OHSS(first trimester only = 4 | no = 0
        public bool OHSS { get; set; }
        //  yes = 1 | no = 0
        public bool currentSystemicInfection { get; set; }
        //  yes = 1 | no = 0
        public bool immobilityOrDehydration { get; set; }


        //public string middleName { get; set; }

        private ApplicationDbContext _dbContext;

        public Patient()
        {
            this._dbContext = new ApplicationDbContext();
        }


        public void AddToDatabase()
        {
            _dbContext.Patients.Add(this);
            _dbContext.SaveChanges();

        }

【问题讨论】:

  • 如果每个yes给出+1,只需迭代所有属性并查看它们是否设置为true并将计数加1。也许这会有所帮助 (stackoverflow.com/questions/8151888/…)
  • 1.创建class Risk { string name; int value; bool checked; } 2. 构建它们的列表... 3. 将列表中的所有值求和int sumOfRisks = list.Where(x=&gt;x.checked).Sum(x=&gt;x.value);
  • 我强烈建议您花额外的时间来分解这个庞大的课程。如果您不熟悉它,请查看单一职责原则 (SRP) 和不要重复自己 (DRY) 原则。
  • @LewsTherin 我已经添加了我的类的样子,但我还没有制作 if-else 语句部分,因为我不确定这是否是正确的方法。我不熟悉你提到的术语,所以我会研究它们
  • @Selvin 我真的不明白我会如何应用它,因为我还在学习,所以我对 c# 不太熟悉

标签: c# asp.net


【解决方案1】:

编写一系列表示每个风险因素的得分量的表达式,并将它们放入一个数组中。

var riskFactors = new Func<Patient,float>[]
{
    p => p.cancer  ? 3.0F : 0F,
    p => p.multiplePregnancy ? 2.0F : 0F
    p => p.caesareanSectionInLabour ? 1.0F : 0.F,
    //One expression per item in the questionnaire
};

那么你只需要将它们相加即可得到患者的总分:

public float GetScoreForPatient(Patient patient)
{
    return riskFactors.Select
    ( 
        factor => factor(patient)
    )
    .Sum();
}

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2019-09-06
    • 1970-01-01
    • 2019-04-09
    • 2021-02-02
    相关资源
    最近更新 更多