【发布时间】:2014-11-11 13:37:56
【问题描述】:
我有一个包含许多属性的类:
public class Report
{
public int reportId { get; set; }
public string sentDate { get; set; }
public string photoName { get; set; }
public string state { get; set; }
public string patientName { get; set; }
public string doctorName { get; set; }
public string sex { get; set; }
public int isPregnant { get; set; }
public float weight { get; set; }
public float height { get; set; }
public int age { get; set; }
public string information { get; set; }
public string response { get; set; }
然后我用参数实现一个构造函数:
public Report(int id, string photoName, string sentDate, string state,string response)
{
this.reportId = id;
this.photoName = photoName;
this.sentDate = sentDate;
this.state = state;
this.response = response;
}
最后我实现了调用这个构造函数的方法:
public static List<Report> GetListReportByEmail(string email)
{
DataTable dt = SqlHelper.ExecuteDataset(
new SqlConnection(Tls.ConnStr),
"spReportGetByEmail",
email).Tables[0];
List<Report> tempItems = new List<Report>();
if (dt.Rows.Count != 0)
{
foreach (DataRow rw in dt.Rows)
{
tempItems.Add(
new Report(
int.Parse(rw["ReportId"].ToString()),
Tls.GetBasicUrl() + "/Zdnn000kjUpload/__zd__MedicalConsultation/"
+ rw["PhotoName"].ToString(),
DateTime.Parse(rw["SentDate"].ToString()).ToString("dd/M/yyyy"),
rw["State"].ToString(),
rw["Response"].ToString()));
}
return tempItems;
}
else
{
return null;
}
}
但是当我检查返回值时,除了构造函数中提到的属性之外,我还发现了其类型为 int 或 float 且值为 0 的所有属性。
我需要这种方法用于 Web 服务。当我测试这个网络服务时,我发现构造函数中提到的属性,还有 int 和 float 没有填充:
<Report>
<reportId>4</reportId>
<sentDate>21/6/2014</sentDate>
<photoName>
http://localhost:2055/Zdnn000kjUpload/__zd__MedicalConsultation/
</photoName>
<state/>
<isPregnant>0</isPregnant>
<weight>0</weight>
<height>0</height>
<age>0</age>
<response/>
</Report>
知道存储过程没问题。
所以谁能帮我只传输构造函数中提到的变量:我不需要传输age,height,weight,isPregnant
谢谢。
【问题讨论】:
-
那么您的构造函数只设置了一些属性 - 您希望填充其余的属性是什么?
-
所见即所得。其他属性没有更新,所以它们是 0。到底是什么问题?
-
用这个方法我只需要填充这些属性,其他的不会填充,但是我在这个方法的返回中找到了。
-
我认为@Himitcho 希望未填充的属性不包含在 xml 中。我在想,为了实现这一点,您可能需要使用 Nullable
作为属性类型。例如 public float? weight { get; set; }.