首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将匿名类型转换为名义类型

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

Stack Overflow用户
提问于 2011-09-09 04:48:07
回答 4查看 497关注 0票数 1

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

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

Stack Overflow用户

发布于 2011-09-09 04:51:59

不要使用anonymus type,可以使用必须返回的类型

代码语言:javascript
运行
复制
 select new Rpt_IncidentWithConfirm 
                   {
                       // set all properties you need
                   }).SingleOrDefault();

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

代码语言:javascript
运行
复制
 return db.Rpt_IncidentWithConfirms.Where( ... ).FirstOrDefault();

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

代码语言:javascript
运行
复制
//do something with incident.Text
return incident.i;
票数 2
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7354288

复制
相关文章

相似问题

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