Sid Broklyn Sid Broklyn - 3 months ago 6
C# Question

Best way to create a entity in c#

I am making a

Model
class which should look like this:

public class ModelSection
{
public int SectionID { get; set; }
public string SectionName { get; set; }
public string SectionReportName { get; set; }
}


I am making a instance of it like this...

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;


Is there a better way to do this? Thanks.

Answer

You can try doing this

 ModelSection ms = new ModelSection {
                    SectionReportName = subreports[0].ToString(),
                    SectionID = SectionID1,
                    SectionName = SectionName1
                    };

Reference: https://msdn.microsoft.com/en-us/library/bb384062.aspx?f=255&MSPPError=-2147217396

Comments