C# 在c中创建实体的最佳方法#
我正在制作一个C# 在c中创建实体的最佳方法#,c#,entity-framework,design-patterns,entity,C#,Entity Framework,Design Patterns,Entity,我正在制作一个模型类,它应该是这样的: public class ModelSection { public int SectionID { get; set; } public string SectionName { get; set; } public string SectionReportName { get; set; } } 我正在做一个这样的例子 ModelSection ms = new ModelSection(); ms.SectionReport
模型
类,它应该是这样的:
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;
有更好的方法吗?您可以像下面这样做
ModelSelection ms1 = new ModelSelection()
{
SectionID = SectionID1,
SectionName = SectionName1,
SectionReportName = subreports[0].ToString()
};
ModelSelection ms2 = new ModelSelection()
{
SectionID = SectionID2,
SectionName = SectionName2,
SectionReportName = subreports[0].ToString()
};
你可以试着这样做
ModelSection ms = new ModelSection {
SectionReportName = subreports[0].ToString(),
SectionID = SectionID1,
SectionName = SectionName1
};
参考资料:请更准确地说明您的要求!你到底想改进什么?我有3个报表需要导入到一个主报表中,我需要做的第一件事是创建这3个子报表的实体。。。现在,我需要硬编码值,但以后我需要使其成为泛型。我不知道该说什么了@CoconutIf博士如果您可以通过索引器访问节ID和名称,您可以将该逻辑放入循环中,并将新创建的ModelSection对象添加到列表中。我选择此选项是因为它看起来更好,但我需要在这方面做更多工作。无论如何,tnx@fabrzio@SidBroklyn检查链接中的集合初始值设定项以处理许多对象的初始化,也许这会很有用。这!!!。。。然后,如果你想创建多个实体,你可以通过一些循环重复这个过程