我只是前幾天寫這些擴展方法:
#region RankBy
public static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, null, false, resultSelector);
}
public static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, comparer, false, resultSelector);
}
public static IEnumerable<TResult> RankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, comparer, true, resultSelector);
}
public static IEnumerable<TResult> RankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.RankBy(keySelector, null, true, resultSelector);
}
private static IEnumerable<TResult> RankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
bool descending,
Func<TSource, int, TResult> resultSelector)
{
comparer = comparer ?? Comparer<TKey>.Default;
var grouped = source.GroupBy(keySelector);
var ordered =
descending
? grouped.OrderByDescending(g => g.Key, comparer)
: grouped.OrderBy(g => g.Key, comparer);
int totalRank = 1;
foreach (var group in ordered)
{
int rank = totalRank;
foreach (var item in group)
{
yield return resultSelector(item, rank);
totalRank++;
}
}
}
#endregion
#region DenseRankBy
public static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, null, false, resultSelector);
}
public static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, comparer, false, resultSelector);
}
public static IEnumerable<TResult> DenseRankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, comparer, true, resultSelector);
}
public static IEnumerable<TResult> DenseRankByDescending<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, int, TResult> resultSelector)
{
return source.DenseRankBy(keySelector, null, true, resultSelector);
}
private static IEnumerable<TResult> DenseRankBy<TSource, TKey, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer,
bool descending,
Func<TSource, int, TResult> resultSelector)
{
comparer = comparer ?? Comparer<TKey>.Default;
var grouped = source.GroupBy(keySelector);
var ordered =
descending
? grouped.OrderByDescending(g => g.Key, comparer)
: grouped.OrderBy(g => g.Key, comparer);
int rank = 1;
foreach (var group in ordered)
{
foreach (var item in group)
{
yield return resultSelector(item, rank);
}
rank++;
}
}
#endregion
可以按如下方式使用它們:
var rankedPlayers = players.RankByDescending(
p => p.Score,
(p, r) => new { Rank = r, Player = p });
RankBy
和DenseRankBy
之間的區別是,RankBy
創建「間隙」(例如1,1,3,3,3,6 ...)而DenseRankBy
不(1,1,2,2,2,3 ...)
爲什麼你的解決方案不好? LINQ可以簡化一些查詢,但有時使用循環的正常命令代碼更好。 LINQ沒有使循環過時。 – svick
您可以發佈您的解決方案,我們可以嘗試使用LINQ簡化(?!)它,但這並不總是有意義 – sll
請參見[C#排名對象,多個條件](http://stackoverflow.com/questions/932300/ c-sharp-ranking-of-objects-multiple-criteria)有幾個很好的解決方案 – sll