我正在制作一个Model
类,看起来像这样:
public class ModelSection
{
public int SectionID { get; set; }
public string SectionName { get; set; }
public string SectionReportName { get; set; }
}
我正在制作这样的实例...
ModelSection ms = new ModelSection();
ms.SectionReportName = subreports[0].ToString();
ms.SectionID = SectionID1;
ms.SectionName = SectionName1;
ModelSection ms1 = new ModelSection();
ms1.SectionReportName = subreports[1].ToString();
ms1.SectionID = SectionID2;
ms1.SectionName = SectionName2;
ModelSection ms2 = new ModelSection();
ms2.SectionReportName = subreports[2].ToString();
ms2.SectionID = SectionID3;
ms2.SectionName = SectionName3;
有更好的方法吗?
答案 0 :(得分:3)
您可以尝试这样做
ModelSection ms = new ModelSection {
SectionReportName = subreports[0].ToString(),
SectionID = SectionID1,
SectionName = SectionName1
};
参考:https://msdn.microsoft.com/en-us/library/bb384062.aspx?f=255&MSPPError=-2147217396
答案 1 :(得分:1)
您可以更像以下内容。
ModelSelection ms1 = new ModelSelection()
{
SectionID = SectionID1,
SectionName = SectionName1,
SectionReportName = subreports[0].ToString()
};
ModelSelection ms2 = new ModelSelection()
{
SectionID = SectionID2,
SectionName = SectionName2,
SectionReportName = subreports[0].ToString()
};