EF / MVC得到非规范化列表

时间:2014-12-26 17:49:51

标签: entity-framework asp.net-mvc-4

我有一个具有多对多关系的模型。我希望获得此模型所代表的数据的非规范化列表,包括链接模型中的大多数数据。

简化模型。

public class NewsItem :  IModel, IMultiTenant
{
    public int Id { get; set; }
    public String Title { get; set; }
    public DateTime PublishDate { get; set; }

    public virtual ICollection<Activity> Activities { get; set; }
    public virtual ICollection<StaffMember> StaffMembers { get; set; }
    public virtual ICollection<Member> Members { get; set; }
}
public class Activity : IMultiTenant, IModel, ILinkedStaffMember, ILinkedAdmin
{
    public int Id { get; set; }
    public String Description { get; set; }
    public virtual ICollection<StaffMember> StaffMembers { get; set; }
}
public class StaffMember : IMultiTenant, IModel, ILinkedActivity
{
    public int Id { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set; }
}
public class Member : IMultiTenant, IModel, ILinkedActivity
{
    public int Id { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set; }
}

Some sample data (that does not make any sense :-) ) :
NewsItem 1: Title "The blue team won", Linked Activities 1,3 Linked Members 3
NewsItem 2: Title "The red team won", Linked Activities 2 Linked Members 1,2
NewsItem 3: Title "The green team won", Linked Activities 1,2 Linked Members 2
NewsItem 4: Title "The oragne team won", Linked Activities 3 Linked Members 1,3

Activity1: Desc : "Football" linked staffmembers 1,3
Activity2: Desc : "Tennis" linked staffmembers 1,2
Activity3: Desc : "Basketball" linked staffmembers 2

Staffmember 1: Mike Rogers
Staffmember 2: Jim Morrison
Staffmember 3: Neil Young

Member 1: Neil Armstrong
Member 2: Youri Gagarin
Member 3: Frank De Winne
member 4: Dirk Frimout

示例结果我正在寻找什么。当我检索一个具有适合项目1和2的虚构过滤条件的newsitems列表时。我希望返回跟随列表。

"The blue team won", "Football", "Mike Rogers", "Frank De Winne"
"The blue team won", "Football", "Neil Young", "Frank De Winne"
"The blue team won", "Basketball", "jim Morrison", "Frank De Winne"
"The red team won", "Tennis", "Mike Rogers", "Neil Armstrong"
"The red team won", "Tennis", "Jim Morrison", "Neil Armstrong"
"The red team won", "Tennis", "Mike Rogers", "Youri Gagarin"
"The red team won", "Tennis", "Jim Morrison", "Youri Gagarin"

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

EF从数据库获取数据的方式看起来像这样。您可以使用EF生成查询:

var res = context.Entities.Include(e=>e.ChildEntity)...

然后执行此操作以拉取查询字符串并自行运行(取自This SO Question):

string sql = ((System.Data.Objects.ObjectQuery)result).ToTraceString();