实现与LINQ密集排名 [英] implement dense rank with linq
本文介绍了实现与LINQ密集排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码LINQ,如何添加DENSE_RANK我的结果吗?如果是这样的速度太慢或复杂的,如何只排名窗函数?
VAR X = tableQueryable
。凡( WHERE条件)
.GroupBy(卡斯特= gt;新建{fieldOne = cust.fieldOne ??的String.Empty,fieldTwo = cust.fieldTwo ??}的String.Empty)
。凡(G => g.Count()> 1)
.ToList()
.SelectMany(G => g.Select(卡斯特= gt;新建{
cust.fieldOne
, cust.fieldTwo
,cust.fieldThree
}));
解决方案
这做了 DENSE_RANK( )
。更改根据您的需要:)
GROUPBY
和订单
基本上, DENSE_RANK
是编号查询的有序组,以便:
VAR DenseRanked = data.Where(项目= GT; item.Field2 == 1)
//分组被通缉键$ b $数据b .GroupBy(项目=>新建{item.Field1,item.Field3,item.Field4})
。凡(@group => @ group.Any())
//现在,我有我决定如何安排团体
的顺序组.OrderBy (@group => @ group.Key.Field1 ??的String.Empty)
.ThenBy(@group => @ group.Key.Field3 ??的String.Empty)
.ThenBy(@组=> @ group.Key.Field4 ??的String.Empty)
//因为LINQ到实体不支持以下选择重载我将它转换为一个IEnumerable - 注意,任何数据我不想让前
.AsEnumerable()
已经被过滤掉//使用选择我有一个索引输入参数的此重载。因为我的工作范围是群体则是该组的排名。该指数从0开始,所以我第一次做++。
。选择((@组,I)=>新建
{
项= @group,
等级= ++ I
})
//我正在寻找的个别项目,而不是群体,所以我用很多选择来检索。此重载给了我两个项目和组 - 这样我就可以得到上面的$ B $创建b .SelectMany(V =>的排名领域; v.Items,(S,I)=>新建
$ { b $ b项目= i,则
DenseRank = s.Rank
})了ToList();
另一种方法是通过的Manoj的回答中的这个问题 - 但我少从表喜欢它,因为选择的两倍
Using the following linq code, how can I add dense_rank to my results? If that's too slow or complicated, how about just the rank window function?
var x = tableQueryable
.Where(where condition)
.GroupBy(cust=> new { fieldOne = cust.fieldOne ?? string.Empty, fieldTwo = cust.fieldTwo ?? string.Empty})
.Where(g=>g.Count()>1)
.ToList()
.SelectMany(g => g.Select(cust => new {
cust.fieldOne
, cust.fieldTwo
, cust.fieldThree
}));
解决方案
This does a dense_rank()
. Change the GroupBy
and the Order
according to your need :)
Basically, dense_rank
is numbering the ordered groups of a query so:
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();
Another way is as specified by Manoj's answer in this question - But I less prefer it because of the selecting twice from the table.
这篇关于实现与LINQ密集排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文