实体框架 - 选择特定的列而不失投返回强类型 [英] Entity Framework - Select specific columns and return strongly typed without losing cast

查看:111
本文介绍了实体框架 - 选择特定的列而不失投返回强类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做同样的事情到<一个href=\"http://stackoverflow.com/questions/1094931/linq-to-sql-how-to-select-specific-columns-and-return-strongly-typed-list\">this帖子在这里我就不从一个特定的实体拉回来的所有列,但我的框架,利用传承的,我失去了实体类型的范围,它被强制转换为匿名类型之后。

I'm trying to do something similar to this post where I don't pull back all columns from a particular entity, however my framework makes use of inheritence and I lose scope of the entity type after it's been cast to an anonymous type.

我的实体框架的结构有一个名为的动作的基本实体。在这里,我已经创建了两个实体继承所谓的事件活动的。我想拉回到最近的X操作并传递给我的它接受的行动,如果它从那里确定一个活动或事件,并呈现正确的局部视图强类型的视图。

The structure of my Entity Framework has a base entity called Action. From here I've created two inherited entities called Event and Activity. I want to pull back the last X Actions and pass them to my strongly typed view which accepts an Action and from there determines if its an Activity or Event and renders the correct partial view.

if(Model.GetType() == typeof(Event))
{
  //render Event view
}
else if(Model.GetType() == typeof(Activity))
{
  //render Activity view
}

我可以拉过去的10作为匿名类型,然后施放:

I can pull the last 10 as an anonymous type and then cast:

var result = from a in new DataContext().Actions
             where a.UserId == someGuidValue
             select new { a.CreatedOn, a.Summary };

List<Action> list = result.AsEnumerable()
                          .Select(o => new Action {
                                           CreatedOn = o.CreatedOn, 
                                           Summary = o.Summary
                          }).ToList();

但是,一旦我通过行动的新的List到我的强类型的观点就会失去它是否是一个活动或者一个事件,因为它被转换为一个行动范围。我的问题是,不暴露鉴别列,有没有办法在每个项目强制转换为正确的类型或者我要对这个错误的方式?

However, once I pass the new List of Actions to my strongly typed view it loses scope of whether it's an Activity or an Event since it's been cast as an Action. My question is, without exposing the discriminator column, is there any way to cast each item to the proper type or am I going about this the wrong way?

推荐答案

一个有点缺憾,但将工作:

A bit kludgy, but will work:

var result = from a in new DataContext().Actions
             where a.UserId == someGuidValue
             let IsEvent = a as Event != null
             select new { a.CreatedOn, IsEvent, a.Summary };

List<Action> list = result.AsEnumerable()
                          .Select(o => o.IsEvent ?
                                           (Action) new Event {
                                               CreatedOn = o.CreatedOn, 
                                               Summary = o.Summary
                                           }
                                           : (Action) new Activity {
                                               CreatedOn = o.CreatedOn, 
                                               Summary = o.Summary
                                           }
                          }).ToList();

与特定类型的列例如 presuming的e.EventSpecific是可空类型的。

var result = from a in new DataContext().Actions
             where a.UserId == someGuidValue
             let ev = a as Event
             let IsEvent = ev != null
             select new { a.CreatedOn, IsEvent, a.Summary, ev.EventSpecific };

List<Action> list = result.AsEnumerable()
                          .Select(o => o.IsEvent ?
                                           (Action) new Event {
                                               CreatedOn = o.CreatedOn, 
                                               Summary = o.Summary,
                                               EventSpecific = o.EventSpecific
                                           }
                                           : (Action) new Activity {
                                               CreatedOn = o.CreatedOn, 
                                               Summary = o.Summary,
                                               EventSpecific = o.EventSpecific // will be null, but using o.EventSpecific saves casting
                                           }
                          }).ToList();

如果o.EventSpecific是一个非空类型,则必须将其转换为在L2E查询可空类型。

If o.EventSpecific is of a non-nullable type, then you must convert it to a nullable type in the L2E query.

这篇关于实体框架 - 选择特定的列而不失投返回强类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆