如何编写通用组

时间:2017-07-29 04:36:19

标签: c# linq

我有以下代码,唯一的区别是它对数据进行分组,是否有任何方法可以通过逐个参数传递将其转换为一个逻辑,因此可以删除重复的代码。

  public interface IStackChartData
{
    IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting);
}

public class DailyStack : IStackChartData
{
    public IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting)
    {
        return chartSetting.StackChartDataPoints.GroupBy(x => new { x.DataTime.Hour, x.RateId })
                                                       .OrderBy(x => x.Key.RateId).Select(cl => new StackChartData(
                                                                             cl.Key.RateId,
                                                                             cl.Key.Hour.ToString(CultureInfo.CurrentCulture),
                                                                             cl.Sum(c => Convert.ToDecimal(c.YData)),
                                                                             chartSetting.GetColorCode(cl.Key.RateId))).ToList();
    }
}

public class MonthlyStack : IStackChartData
{
    public IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting)
    {
        return chartSetting.StackChartDataPoints.GroupBy(x => new { x.DataTime.Date, x.RateId })
                                    .OrderBy(x => x.Key.RateId).Select(cl => new StackChartData(
                                                                  cl.Key.RateId,
                                                                  cl.Key.Date.Day.ToString(CultureInfo.CurrentCulture),
                                                                  cl.Sum(c => Convert.ToDecimal(c.YData)),
                                                                  chartSetting.GetColorCode(cl.Key.RateId))).ToList();
    }
}

public class YearlyStack : IStackChartData
{
    public IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting)
    {
        return chartSetting.StackChartDataPoints.GroupBy(x => new { x.DataTime.Month, x.RateId })
                                        .OrderBy(x => x.Key.RateId).Select(cl => new StackChartData(
                                                                                  cl.Key.RateId,
                                                                                  cl.Key.Month.ToString(CultureInfo.CurrentCulture),
                                                                                  cl.Sum(c => Convert.ToDecimal(c.YData)),
                                                                                  chartSetting.GetColorCode(cl.Key.RateId))).ToList();
    }
}

1 个答案:

答案 0 :(得分:1)

假设x.DataTimeDateTime,那么您的实施之间唯一不同的是您可能有int Monthint HourDateTime Date作为您的单位时间。您需要调用文化敏感转换,它很方便地成为界面的一部分IConvertible。因此,您可以使用static编写一个Func方法来选择此数据:

internal static IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting, Func<DateTime, IConvertible> selector)
{
    return chartSetting.StackChartDataPoints.GroupBy(x => new { TimeUnit=selector(x.DataTime), x.RateId })
                                                   .OrderBy(x => x.Key.RateId).Select(cl => new StackChartData(
                                                                         cl.Key.RateId,
                                                                         cl.Key.TimeUnit.ToString(CultureInfo.CurrentCulture),
                                                                         cl.Sum(c => Convert.ToDecimal(c.YData)),
                                                                         chartSetting.GetColorCode(cl.Key.RateId))).ToList();
}

然后调用变得简单:

public class DailyStack : IStackChartData
{
    public IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting)
    {
        return MyUtilities.GetDetails(chartSetting, x => x.Hour);
    }
}

public class MonthlyStack : IStackChartData
{
    public IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting)
    {
        return MyUtilities.GetDetails(chartSetting, x => x.Date);
    }
}

public class YearlyStack : IStackChartData
{
    public IReadOnlyCollection<StackChartData> GetDetails(StackChartSetting chartSetting)
    {
        return MyUtilities.GetDetails(chartSetting, x => x.Month);
    }
}