将匿名类型转换为名义类型
我这里有一种方法应该返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以直接在查询表达式中实例化 Rpt_IncidentWithConfirm 对象,并仅引用您需要的数据库值:
You can instantiate the
Rpt_IncidentWithConfirm
object directly in the query expression and only refer to the database values you need:不要使用
匿名类型
,您可以使用必须返回编辑的类型:
如果您的查询是要返回的集合类型,您可以简单地使用查询结果:
或者如果您需要文本值,请使用:
Don't use
anonymus type
you can use the type you have to returnEdit:
If your query is on collection type you want to return you can simply use result of query:
or if u need value of text use :
我实际上使用下面链接的答案来解决我的问题。这并不完全是我想要做的,但我现在不必手动输入所有内容。
返回匿名类型结果?
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?
您不需要在选择中创建匿名类型,只需创建命名类型的对象即可
you are not required to create an anonymous type in the select just create an object of the the named type instead