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

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

问题描述

我正在尝试类似于这篇文章,我不拉回特定实体的所有列,但是我的框架使用了继承,并且在将其转换为匿名类型之后,我将失去实体类型的范围。

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.

我的实体框架的结构有一个名为 Action 的基础实体。从这里我创建了两个继承的实体,称为事件活动。我想拉回最后的X Actions并将其传递给我强力类型的视图,它接受一个Action,从那里确定它是一个Activity还是Event,并呈现正确的部分视图。

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();

但是,一旦我将新的操作列表传递给我的强类型视图,它将失去范围一个活动或事件,因为它被转换为一个Action。我的问题是,在没有暴露歧视列的情况下,是否有任何方式将每个项目投射到正确的类型,或者我以这种方式进行错误的处理?

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?

推荐答案

有点kludgy,但会工作:

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();

假定e.EventSpecific是可空类型的类型专用列的示例。

Example with type-specific columns, presuming that e.EventSpecific is of a nullable type.

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天全站免登陆