属性或索引器无法分配 – 它是只读的
我在数据库中捕获出生日期和诊断日期,并希望按诊断日期计算年龄。
这是我的代码。 但我有错误跟随错误。
错误2属性或索引器’LightSwitchApplication.Patient.AgeAtDiagnosis’无法分配 – 它是只读的
partial void AgeAtDiagnosis_Compute(ref int result) { // Set result to the desired field value AgeAtDiagnosis = DateofDiagnosis.Year - DateofBirth.Year; if (DateofBirth > DateofDiagnosis.AddYears(-AgeAtDiagnosis)) { AgeAtDiagnosis--; } }
DateTime birth = new DateTime(1950, 01, 01); DateTime diagnosis = new DateTime(2012, 02, 01); TimeSpan Span = diagnosis - birth; DateTime Age = DateTime.MinValue + Span; // note: MinValue is 1/1/1 so we have to subtract... int Years = Age.Year - 1;
TimeSpane age0 = this.DateOfDiagnosis - this.DateOfBirth; int age1 = (int) Math.Trunc( age0.TotalDays / 365.25);
您可以从另一个日期中减去一个日期以提供TimeSpan。 它有一个TotalDays属性,您可以从中获得年份。 这取决于你想要的准确度:
var ageInDays = (diagnoticDate - dateOfBirth).TotalDays; var age = Convert.ToInt32(ageInDays / 365);
显然,闰年会导致问题,因此另一种选择是检查每个日期的年,月,日属性并单独计算它们的差异,同时考虑部分年份等
- 如何使用ExecuteScalar从插入的行中获取生成的id?
- 无法从pocketpc打开与SQL Server 2005的连接(Windows Mobile 6)
- 使用ODP.NET按名称绑定查询参数
- 在C#中创建(按需)SQL Server 2008 Express数据库的最佳实践?
- 使用ADO.NETentity framework时出错
- 从批处理SQL命令(SQL Server)检索“输出消息”
- 为什么SqlParameter名称/值构造函数将0视为null?
- 将数据异步加载到Windows窗体中的DataTable中
- 如何在给定DbCommand或DbConnection的情况下创建DbDataAdapter?