-1

在我的 EF 模型中: 在此处输入图像描述

我提出这样的看法:

在此处输入图像描述

使用以下 LINQ 查询(在您的帮助下):

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

但我有两个问题:

1. 如果同一步骤中的 2 名成员(例如 35 岁之前)具有相同的时间(最多毫秒),则他们必须具有相同的位置

2. 在结果中的成员子集合中,我想按 StepId 排列成员并显示 StepName。

如何修改此查询?谢谢。

编辑1:

我想拿一些这个:

在此处输入图像描述

4

1 回答 1

1

SQL Query 计算正确的成员位置:

select *, DENSE_RANK() over (partition by StepId order by Time)
from Members m
join Data d on m.MemberId =d.MemberId

linq 等效于现有的 linq 查询:

var query = context.Data.ToList()
    .OrderBy(d=>d.Time)
    .GroupBy(d => d.Members.StepId)
    .SelectMany(g => 
        g.OrderBy(d => d.Time)
        .GroupBy(d => d.Time)
        .SelectMany((x, place) => 
            x.Select(d => 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, 
                StepName = d.Members.Steps.StepName, 
                StepId = d.Members.Steps.StepId 
            }).OrderBy(x=>x.StepId),
        TotalTime = g.Aggregate(new TimeSpan(), (sum, nextData) => sum.Add(nextData.Time))
    });
于 2012-11-23T19:55:26.093 回答