Calculate years in a POCO Calculated Property
我假设我很接近。我有一个值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | namespace OrlandoAppraiser.Models { public class Appraiser { public int AgentID { get; set; } public string Name { get; set; } public string LicenseNum { get; set; } public DateTime StartedAgent { get; set; } public string YearsAsAgent { get { return (Math.Floor((DateTime.Now - StartedRealEstate).TotalDays / 365.25D)); } } } } |
我看了一些不同的答案,但是我很难找到一种在计算属性中进行这种简单操作的方法。我知道这不应该有太大的不同,但我的代码有错误。
simplistic this is a漂亮的方法。我肯定你呼叫
1 2 3 4 | public string YearsAsAgent { get { return (DateTime.Now.Year - StartedRealEstate.Year).ToString(); } } |
this should help。修饰version of this。P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | DateTime _startedRealEstate = new DateTime(2012, 11, 15); public DateTime StartedRealEstate { get { return _startedRealEstate; } set { _startedRealEstate = value; } } public int YearsAsAgent { get { DateTime zeroTime = new DateTime(1, 1, 1); TimeSpan span = DateTime.Now - StartedRealEstate; int years = (zeroTime + span).Year - 1; return years; } } private void button1_Click_2(object sender, EventArgs e) { int totalYears = YearsAsAgent; } |