我有一个带有计算参数的类:
public class Job
{
public Job()
{
Reports = new List<Report>();
}
[Key]
public int JobID { get; set; }
public ICollection<Report> Reports { get; set; }
public DateTime AppointmentDate { get; set; }
public decimal AverageReportTurnaround
{
get
{
DateTime reportdate = Reports.Select(x=>x.DateCompleted).FirstOrDefault();
return (reportdate - AppointmentDate).Value.Days;
}
}
如何将datefrom字段传递给构造函数,以便它只计算reportdate为&gt;的AverageReportTurnaround。 datefrom?
答案 0 :(得分:1)
您可以使用私人字段
public class Job
{
private DateTime fromDate;
public Job(DateTime fromDate)
{
Reports = new List<Report>();
this.fromDate = fromDate;
}
...
public decimal AverageReportTurnaround
{
get
{
DateTime reportdate = Reports.Where(x => x.DateCompleted > this.fromDate)
.Select(x=> x.DateCompleted).FirstOrDefault();
return (reportdate - AppointmentDate).Value.Days;
}
}
}
答案 1 :(得分:0)
你可以这样做:
public class Job
{
public Job(DateTime dateFrom)
{
Reports = new List<Report>();
DateFrom = dateFrom;
}
[Key]
public int JobID { get; set; }
public ICollection<Report> Reports { get; set; }
public DateTime AppointmentDate { get; set; }
public DateTime DateFrom { get; private set; }
public decimal AverageReportTurnaround
{
get
{
DateTime reportdate = Reports.Where(x => x.ReportDate > DateFrom).Select(x=>x.DateCompleted).FirstOrDefault();
return (reportdate - AppointmentDate).Value.Days;
}
}
但是,如果您在EntityFramework中使用此类(根据&#39; Key&#39;属性判断),则应将AverageReportTurnaround转换为使用dateFrom作为参数的方法