处理排名数据w / C#& LINQ

时间:2010-03-05 20:04:25

标签: c# linq linq-to-objects

我在列表中有一个对象,我需要对几种不同的方法进行排序。目前代码相当笨重,因为它要求我单独处理每一列。例如:

public class Data
{
    public int AValue { get; set; }
    public int ARanking { get; set; }
    public int BValue { get; set; }
    public int BRanking { get; set; }
    public int CValue { get; set; }
    public int CRanking { get; set; }
}

public class Container
{
    public List<Data> RankingData { get; set; }

    public void RankData()
    {
        int count = 1;

        foreach (Data item in RankingData.OrderBy(d => d.AValue))
        {
            item.ARanking = count;
            count++;
        }

        count = 1;

        foreach (Data item in RankingData.OrderBy(d => d.BValue))
        {
            item.BRanking = count;
            count++;
        }

        count = 1;

        foreach (Data item in RankingData.OrderBy(d => d.CValue))
        {
            item.CRanking = count;
            count++;
        }
    }
}

我想解决的问题是我想写一些大致如下:

public void RankData<V, R>()
{
    int count = 1;

    foreach(Data item in RankingData.OrderBy(V))
    {
        item.R = count;
        count++;
    }
}

因为我需要改变排名逻辑(例如,处理打破规则),我会编写一次代码,而不是复制代码20次以使规则匹配。我错过了什么?

更新

使用Tanzelax的解决方案作为基础,这是我提出的扩展类:

public static class RankingExtension
{
    public static void SetRanking<TKey>(this List<Data> dataSet, bool Ascending, Func<Data, TKey> getOrderBy, Action<Data, int> setRank)
        where TKey : IComparable
    {
        var ordered = (Ascending) ? dataSet.OrderBy(getOrderBy) : dataSet.OrderByDescending(getOrderBy);

        int i = 1;
        foreach (Data item in ordered)
        {
            setRank(item, i);
            i++;
        }
    }
}

我必须添加一个开关,以便我可以控制该字段是否按升序排序。在我的测试场景中,它会产生适当的输出:

    List<Data> test = new List<Data>();
    test.Add(new Data { AValue = 25, BValue = 1.25, CValue = 99.99 });
    test.Add(new Data { AValue = 89, BValue = 2.10, CValue = 1.01 });
    test.Add(new Data { AValue = 10, BValue = 6, CValue = 45.45 });
    test.Add(new Data { AValue = 15, BValue = 2.33, CValue = 2.99 });
    test.Add(new Data { AValue = 90, BValue = 5.43, CValue = 27.89 });

    test.SetRanking(false, d => d.AValue, (d, i) => d.ARank = i);
    test.SetRanking(false, d => d.BValue, (d, i) => d.BRank = i);
    test.SetRanking(true, d => d.CValue, (d, i) => d.CRank = i);

3 个答案:

答案 0 :(得分:1)

未经过测试,但是这样:

void SetRanking(this List<Data> dataSet, Func<Data,int> getOrderBy, Action<Data,int> setRank)
{
    var ordered = dataSet.OrderBy(getOrderBy).ToArray();

    int i = i;
    foreach (Data item in ordered)
    {
        setRank(item, i);
        i++;
    }
}

RankingData.SetRanking(d => d.AValue, (d,i) => d.ARanking = i);
RankingData.SetRanking(d => d.BValue, (d,i) => d.BRanking = i);
RankingData.SetRanking(d => d.CValue, (d,i) => d.CRanking = i);

答案 1 :(得分:1)

这类似于Tanzelax的答案,但它是一种通用的扩展方法。

public static void RankData<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Action<TSource, int> rankSetter
)
{
    int count = 1;
    foreach (var item in source.OrderBy(keySelector))
    {
        rankSetter(item, count);
        ++count;
    }
}

它也会被称为类似于Tanzelax的回答。

RankingData.RankData(d => d.AValue, (d,i) => d.ARanking = i);

答案 2 :(得分:0)

传入返回排名键的Func<Data,K>。 K应该实现IComparable

public static void Rank<K>( IEnumerable<Data> source, Func<Data,K> rankBy ) where K : IComparable
{
   int count = 1;
   foreach (var item in source.OrderBy( rankBy ))
   {
       item.R = count;
       ++count;
   }
}