首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在c#中创建实体的最佳方法

在c#中创建实体的最佳方法
EN

Stack Overflow用户
提问于 2016-08-25 22:19:42
回答 2查看 58关注 0票数 2

我正在制作一个Model类,它应该如下所示:

代码语言:javascript
复制
public class ModelSection
{
    public int SectionID { get; set; }
    public string SectionName { get; set; }
    public string SectionReportName { get; set; }
}

我做了一个这样的例子...

代码语言:javascript
复制
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;

有没有更好的方法来做这件事?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-25 22:28:03

你可以试着这样做

代码语言:javascript
复制
 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

票数 3
EN

Stack Overflow用户

发布于 2016-08-25 22:23:29

你可以像下面这样做。

代码语言:javascript
复制
ModelSelection ms1 = new ModelSelection()
        { 
            SectionID = SectionID1,
            SectionName = SectionName1,
            SectionReportName = subreports[0].ToString()
        };

ModelSelection ms2 = new ModelSelection()
        { 
            SectionID = SectionID2,
            SectionName = SectionName2,
            SectionReportName = subreports[0].ToString()
        };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39147827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档