-1

我遵循 EF 模型:

在此处输入图像描述

我想在以下视图中获取数据:

在此处输入图像描述

在数据实体中,时间是以毫秒为单位的时间跨度。结果,PlaceInStep是成员之间的位置 by StepId

如何进行这个硬查询?

编辑 1

我的一些代码:

var query = from data in ctx.Data
                            join member in ctx.Members
                            on data.MemberId equals member.MemberId
                            join team in ctx.Teams
                            on member.TeamId equals team.TeamId
                            group member by member.TeamId into g
                            select new {TeamId = g.Key, TeamName = g.Select(t=>t.Teams.TeamName), TotalTime = ???};
4

2 回答 2

3

一开始的建议。您不应该在查询中使用 join。您的模型具有导航属性,请改用它们。您可以阅读 Craig Stuntz 的博

您可以在内部列表中选择所有团队和有关成员的信息。试试这个:

var query = ctx.data.ToList().OrderBy(d => d.Time).
            GroupBy(d => d.Members.StepId).
            SelectMany(g => g.Select((d, place) => new { Time = d.Time, Members = d.Members, PlaceInStep = place + 1 })).
            GroupBy(d => d.Members.TeamId).
            Select(g => new 
            {
               TeamId = g.Key, 
               Name = g.Select(d => d.Members.Teams.TeamName).First(),  
               Members = g.Select(d => new {Time = d.Time, PlaceInStep = d.PlaceInStep, MemberName = d.Members.MemberName}),                    
               TotalTime = g.Aggregate(new TimeSpan(), (sum, nextData) => sum.Add(nextData.Time))
            });
于 2012-11-21T10:42:54.977 回答
1

试试这个:

var query = ctx.Data.Join(ctx.Members, d => d.MemberId, m => m.MemberId, (d,m)=>new {data=d, member = m})
                .Join(ctx.Teams, dm=>dm.member.TeamId, t=>t.TeamId, (dm, t)=>new{data=dm.data, member=dm.member, team = t})
                .GroupBy(dmt=>new {dmt.team.TeamId, dmt.team.TeamName})
                .Select(g=>new 
                           {
                               TeamId = g.Key.TeamId, 
                               TeamName = g.Key.TeamName, 
                               TotalTime = g.Aggregate(0, (sum, gr)=>sum.Add(gr.data.Time))
                           });
于 2012-11-21T11:00:25.140 回答