2015-05-05 47 views
7

我有一个SQL查询,按多个字段对参与者进行排名。我需要将其转换为LINQ,我知道它没有排名功能。 有人可以帮助转换吗?在LINQ to Entities中的SQL排名

如果有帮助,这是它的功能。此查询从参赛名单中抽取参赛者,并根据RANK() OVER (ORDER BY W desc, L asc, RW asc, RL desc, HP desc, TB desc) AS RANK中列出的字段进行排名。接下来我抢只有那些排名1或2 Where q1.RANK in ('1','2'),看看是否有针对这两个排名Having count(q1.ParticipantID) > 1

Select q1.RANK, count(q1.ParticipantID) as 'Count' 
From (
     Select Distinct ParticipantID, RANK() OVER (ORDER BY W desc, L asc, RW asc, RL desc, HP desc, TB desc) AS RANK 
     From vGroupStandings 
     Where CompetitionID = 8 
     and GroupNumber = 1 
     and EventID = 6 
    ) as q1 
Where q1.RANK in ('1','2') 
Group By q1.RANK 
Having count(q1.ParticipantID) > 1 

UPDATE

这里任何关系的数据选择的所有字段

enter image description here

下面是子查询中过滤数据的示例。从集合中,我一眼就看是否有超过1个战绩排在1级或2级

enter image description here

响应

感谢您的答复,到目前为止,我会让你知道我什么时候可以试试这些。 这是另一个问题。改为从控制器调用存储过程会更好吗?这样我可以保留原来的SQL查询。 我有一些大的查询,我将不得不运行涉及到排名。我想知道是否比在LINQ中重写所有内容更容易。

+0

我试着用Linqer (它会将SQL转换为LINQ为你),并告诉我RANK()转换不受支持。 我对Linq很新,这比我现在可以处理的更复杂。 – madvora

+0

@Hogan,分区部分是可选的 – Alex

+0

好吧,我看到了 - 它使用顺序中的项目来查找关系。 – Hogan

回答

1

就像这样...这是伪代码....我没有测试。

如果你给任何示例数据和预期输出的话,我会做这样

var inner = datasource 
       .OrderByDesc(x => x.W) 
       .ThenBy(x => x.L) 
       // etc for all orders you need 
       .GroupBy(new { W = W, L = L, RW = RW, RL = RL, HP = HP, TB = TB }) 
       .First(2); 

if (inner[0].Count > 1 || inner[1].Count[1] > 1) 
{ 
    Console.Writeline("1 "+inner[0].Count.ToString()); 
    Console.Writeline("2 "+inner[1].Count.ToString()); 
} 
+0

好吧,我添加了一个更新,以便您对我正在谈论的内容有一个视觉感受。如果在等级1或等级2处有任何关系,结果就会过滤掉该子查询。在这种情况下,查询结果将是空白的(因为没有关系)。 – madvora

+0

@madvora - 得到了所有。这也是上面的代码所做的。 – Hogan

2

这是很丑陋,但使用此示例类为我的作品。

public class Participant 
{ 
    public int Id { get; set; } 
    public int Score1 { get; set; } 
    public int Score2 { get; set; } 
    public int ExpectedRank { get; set; } 
} 

在此集合:

var participants = new Participant[] { 
    new Participant { Id = 1, Score1 = 2, Score2 = 5, ExpectedRank = 6 }, 
    new Participant { Id = 2, Score1 = 10, Score2 = 8, ExpectedRank = 1 }, 
    new Participant { Id = 3, Score1 = 7, Score2 = 2, ExpectedRank = 4 }, 
    new Participant { Id = 4, Score1 = 7, Score2 = 4, ExpectedRank = 3 }, 
    new Participant { Id = 5, Score1 = 7, Score2 = 2, ExpectedRank = 4 }, 
    new Participant { Id = 6, Score1 = 7, Score2 = 7, ExpectedRank = 2 }, 
}; 

通过执行以下操作相当丑陋的LINQ查询:

var ranked = participants 
    .OrderByDescending(p => p.Score1) 
    .ThenByDescending(p => p.Score2) 
    .Select((p, i) => new { Order = 1 + i, Participant = p }) 
    .GroupBy(p => new { p.Participant.Score1, p.Participant.Score2 }) 
    .SelectMany(g => g.Select(p => new { 
     Id = p.Participant.Id, 
     Rank = g.Min(x => x.Order), 
     ExpectedRank = p.Participant.ExpectedRank 
    })); 

foreach (var p in ranked) 
    Console.WriteLine(p); 

将会产生以下的输出:

{ Id = 2, Rank = 1, ExpectedRank = 1 } 
{ Id = 6, Rank = 2, ExpectedRank = 2 } 
{ Id = 4, Rank = 3, ExpectedRank = 3 } 
{ Id = 3, Rank = 4, ExpectedRank = 4 } 
{ Id = 5, Rank = 4, ExpectedRank = 4 } 
{ Id = 1, Rank = 6, ExpectedRank = 6 }