在这里寻找一些一般性的指导。基本上我正在使用EF Code First MVC模型构建一个项目,并且我有一个包含许多方法的类,我想使用If语句在其他两个方法中基于值设置一个方法的值。
我搜索过每一个地方,一直在看书,做教程等,但似乎没有人能够覆盖我想要做的事情。我怀疑我错过了一些明显的东西,或者我无法实现我正在做的事情。
以下代码的摘录 - 我试图根据概率和严重性字段中的值设置评级字段的值。我已经尝试修改get和set语句,在构造函数中设置等。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PSA.Models
{
public class Issue
{
public virtual int IssueID { get; set; }
[Required(ErrorMessage = "Title is Required.")]
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual string Type { get; set; }
public virtual string Classification { get; set; }
public virtual bool Internal { get; set; }
public virtual int ProbableStatusId { get; set; }
public virtual ProbableStatus ProbableStatus{ get; set; }
public virtual int SeverityStatusId { get; set; }
public virtual SeverityStatus SeverityStatus { get; set; }
[Display(Name = "Due Date")]
[DataType(DataType.Date)]
public virtual DateTime DueDate { get; set; }
[Display(Name = "Assigned To")]
public int StakeholderID { get; set; }
public virtual Stakeholder Stakeholder { get; set; }
[Display(Name= "Customer")]
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
[DataType(DataType.Date)]
[ScaffoldColumn(false)]
public virtual DateTime DateSubmitted { get; set; }
public virtual string Rating { get; set; }
// Constructor used to initialise any new instance of Date Submitted with System Date and Time
public Issue()
{
this.DateSubmitted = DateTime.Now;
}
}
}
答案 0 :(得分:1)
您可以创建计算属性:
[NotMapped]
public virtual SeverityStatus SeverityStatus
{
get
{
// Implement your calculation logic here
if (ProbableStatusId == 1 && SeverityStatusId == 1)
return new SeverityStatus();
// ...
}
}
注意: 您不能在构造函数中执行此操作,因为两个相关属性为null。另一种解决方案是创建方法
答案 1 :(得分:0)
谢谢大家 - 答案是设置条件并在get语句中返回。很简单我真的不明白get语句对我做了什么。
代码如下所示:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace PSA.Models
{
public class Issue
{
public virtual int IssueID { get; set; }
[Required(ErrorMessage = "Title is Required.")]
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual string Type { get; set; }
public virtual string Classification { get; set; }
public virtual bool Internal { get; set; }
public virtual int ProbableClassificationId { get; set; }
public virtual ProbableClassification ProbableClassification { get; set; }
public virtual int SeverityClassificationId { get; set; }
public virtual SeverityClassification SeverityClassification { get; set; }
string ratingstatus;
public virtual string Rating
{
get
{
if (ProbableClassification != null && SeverityClassification != null)
{
if (SeverityClassification.Severity == "Minor")
{
ratingstatus = "Low";
}
if (ProbableClassification.Probability == "Not Likely" && SeverityClassification.Severity == "Medium")
{
ratingstatus = "Low";
}
if (ProbableClassification.Probability == "Likely" && SeverityClassification.Severity == "Medium")
{
ratingstatus = "Medium";
}
if (ProbableClassification.Probability == "Definitely" && SeverityClassification.Severity == "Medium")
{
ratingstatus = "Medium";
}
if (ProbableClassification.Probability == "Not Likely" && SeverityClassification.Severity == "Major")
{
ratingstatus = "Low";
}
if (ProbableClassification.Probability == "Likely" && SeverityClassification.Severity == "Major")
{
ratingstatus = "High";
}
if (ProbableClassification.Probability == "Definitely" && SeverityClassification.Severity == "Major")
{
ratingstatus = "Crtical";
}
}
return ratingstatus;
}
set
{
ratingstatus = value;
}
}
[Display(Name = "Due Date")]
[DataType(DataType.Date)]
public virtual DateTime DueDate { get; set; }
[Display(Name = "Assigned To")]
public int StakeholderID { get; set; }
public virtual Stakeholder Stakeholder { get; set; }
[Display(Name = "Customer")]
public int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
[DataType(DataType.Date)]
[ScaffoldColumn(false)]
public virtual DateTime DateSubmitted { get; set; }
public Issue()
{
this.DateSubmitted = DateTime.Now;
}
}
}