Это dense_rank()
. Изменение GroupBy
и Order
в соответствии с вашими потребностями :) В принципе, dense_rank
в нумерации упорядоченных групп запроса так:
var DenseRanked = data.Where(item => item.Field2 == 1)
//Grouping the data by the wanted key
.GroupBy(item => new { item.Field1, item.Field3, item.Field4 })
.Where(@group => @group.Any())
// Now that I have the groups I decide how to arrange the order of the groups
.OrderBy(@group => @group.Key.Field1 ?? string.Empty)
.ThenBy(@group => @group.Key.Field3 ?? string.Empty)
.ThenBy(@group => @group.Key.Field4 ?? string.Empty)
// Because linq to entities does not support the following select overloads I'll cast it to an IEnumerable - notice that any data that i don't want was already filtered out before
.AsEnumerable()
// Using this overload of the select I have an index input parameter. Because my scope of work is the groups then it is the ranking of the group. The index starts from 0 so I do the ++ first.
.Select((@group , i) => new
{
Items = @group,
Rank = ++i
})
// I'm seeking the individual items and not the groups so I use select many to retrieve them. This overload gives me both the item and the groups - so I can get the Rank field created above
.SelectMany(v => v.Items, (s, i) => new
{
Item = i,
DenseRank = s.Rank
}).ToList();
Другим способ, как указано ответ Маноджа в this question - Но я менее предпочитаю это, потому что выбора дважды из таблицы.
Ваш метод GroupBy не имеет смысла. Разве вы не должны указывать Func для извлечения ключа? –
Что такое 'dense_rank'? – user3185569
@TimothyGhanem Можете ли вы объяснить PLS? Я придерживался того же синтаксиса, что и http://www.codeducky.org/sql-queries-in-linq/#group-by –