将匿名类型转换为名义类型

发布于 2024-12-03 22:22:58 字数 943 浏览 0 评论 0原文

我这里有一种方法应该返回 Rpt_IncidentWithConfirm 对象,但我不知道如何轻松地将其转换为对象。我知道它如何做我下面的事情的唯一方法是非常低效的。

    public Rpt_IncidentWithConfirm GetIncident(string IncidentID)
    {
        db = new IncidentsDataContext();

        var incident = (from i in db.Rpt_IncidentWithConfirms
                       join d in db.DropDowns on i.incidentType equals d.value
                       where i.incidentID == Convert.ToInt32(IncidentID)
                       select new
                       {
                           i, d.text
                       }).SingleOrDefault();
        Rpt_IncidentWithConfirm r = new Rpt_IncidentWithConfirm();
        // I didn't want to have to type all this here because I have too many fields to map.

        r.bhaIncident = incident.i.bhaIncident;
        r.bitType = incident.i.bitType;
        r.Bottom_Connection = incident.i.Bottom_Connection;
        // And so on.


        return r;
    }

I have a method here that should return a Rpt_IncidentWithConfirm object but I have no clue how to easily convert it to one. The only way I know how it do do what I have below which is very inefficient.

    public Rpt_IncidentWithConfirm GetIncident(string IncidentID)
    {
        db = new IncidentsDataContext();

        var incident = (from i in db.Rpt_IncidentWithConfirms
                       join d in db.DropDowns on i.incidentType equals d.value
                       where i.incidentID == Convert.ToInt32(IncidentID)
                       select new
                       {
                           i, d.text
                       }).SingleOrDefault();
        Rpt_IncidentWithConfirm r = new Rpt_IncidentWithConfirm();
        // I didn't want to have to type all this here because I have too many fields to map.

        r.bhaIncident = incident.i.bhaIncident;
        r.bitType = incident.i.bitType;
        r.Bottom_Connection = incident.i.Bottom_Connection;
        // And so on.


        return r;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心病无药医 2024-12-10 22:22:58

您可以直接在查询表达式中实例化 Rpt_IncidentWithConfirm 对象,并仅引用您需要的数据库值:

var incident = (from i in db.Rpt_IncidentWithConfirms
                join d in db.DropDowns on i.incidentType equals d.value
                where i.incidentID == Convert.ToInt32(IncidentID)
                select new Rpt_IncidentWithConfirm
                {
                   bhaIncident = i.bhaIncident
                 , bitType = i.bitType
                 , Bottom_Connection = i.Bottom_Connection
                }).SingleOrDefault();

You can instantiate the Rpt_IncidentWithConfirm object directly in the query expression and only refer to the database values you need:

var incident = (from i in db.Rpt_IncidentWithConfirms
                join d in db.DropDowns on i.incidentType equals d.value
                where i.incidentID == Convert.ToInt32(IncidentID)
                select new Rpt_IncidentWithConfirm
                {
                   bhaIncident = i.bhaIncident
                 , bitType = i.bitType
                 , Bottom_Connection = i.Bottom_Connection
                }).SingleOrDefault();
夏至、离别 2024-12-10 22:22:58

不要使用匿名类型,您可以使用必须返回

 select new Rpt_IncidentWithConfirm 
                   {
                       // set all properties you need
                   }).SingleOrDefault();

编辑的类型:
如果您的查询是要返回的集合类型,您可以简单地使用查询结果:

 return db.Rpt_IncidentWithConfirms.Where( ... ).FirstOrDefault();

或者如果您需要文本值,请使用:

//do something with incident.Text
return incident.i;

Don't use anonymus type you can use the type you have to return

 select new Rpt_IncidentWithConfirm 
                   {
                       // set all properties you need
                   }).SingleOrDefault();

Edit:
If your query is on collection type you want to return you can simply use result of query:

 return db.Rpt_IncidentWithConfirms.Where( ... ).FirstOrDefault();

or if u need value of text use :

//do something with incident.Text
return incident.i;
長街聽風 2024-12-10 22:22:58

我实际上使用下面链接的答案来解决我的问题。这并不完全是我想要做的,但我现在不必手动输入所有内容。
返回匿名类型结果?

        public class IncidentWithDropDown
    {
        public Rpt_IncidentWithConfirm Incident { get; set; }
        public string IncidentTypeText { get; set; }
    }
    public IncidentWithDropDown GetIncident(string IncidentID)
    {
        db = new IncidentsDataContext();

        var incident = (from i in db.Rpt_IncidentWithConfirms
                       join d in db.DropDowns on i.incidentType equals d.value
                       where i.incidentID == Convert.ToInt32(IncidentID)
                       select new IncidentWithDropDown()
                       {
                           Incident = i,
                           IncidentTypeText = d.text
                       }).SingleOrDefault();

        return incident;
    }

I actually used the answer linked below to solve my problem. It isn't exactly what I wanted to do but I don't have to type everything manually now.
Return anonymous type results?

        public class IncidentWithDropDown
    {
        public Rpt_IncidentWithConfirm Incident { get; set; }
        public string IncidentTypeText { get; set; }
    }
    public IncidentWithDropDown GetIncident(string IncidentID)
    {
        db = new IncidentsDataContext();

        var incident = (from i in db.Rpt_IncidentWithConfirms
                       join d in db.DropDowns on i.incidentType equals d.value
                       where i.incidentID == Convert.ToInt32(IncidentID)
                       select new IncidentWithDropDown()
                       {
                           Incident = i,
                           IncidentTypeText = d.text
                       }).SingleOrDefault();

        return incident;
    }
刘备忘录 2024-12-10 22:22:58

您不需要在选择中创建匿名类型,只需创建命名类型的对象即可

public Rpt_IncidentWithConfirm GetIncident(string IncidentID)
    {
        db = new IncidentsDataContext();

        var return (from i in db.Rpt_IncidentWithConfirms
                       join d in db.DropDowns on i.incidentType equals d.value
                       where i.incidentID == Convert.ToInt32(IncidentID)
                       select new Rpt_IncidentWithConfirm(){

                           bhaIncident =i.bhaIncident,
                           bitType = i.bitType,
                           Bottom_Connection = i.Bottom_Connection,
                           // And so on.
                        }).SingleOrDefault()
    }

you are not required to create an anonymous type in the select just create an object of the the named type instead

public Rpt_IncidentWithConfirm GetIncident(string IncidentID)
    {
        db = new IncidentsDataContext();

        var return (from i in db.Rpt_IncidentWithConfirms
                       join d in db.DropDowns on i.incidentType equals d.value
                       where i.incidentID == Convert.ToInt32(IncidentID)
                       select new Rpt_IncidentWithConfirm(){

                           bhaIncident =i.bhaIncident,
                           bitType = i.bitType,
                           Bottom_Connection = i.Bottom_Connection,
                           // And so on.
                        }).SingleOrDefault()
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文