使用c#和linq中的泛型参数重构类似的方法

时间:2015-06-29 13:45:07

标签: c# linq generics

除了第一个参数外,我有两种方法完全相同。我不想重复重复的代码。我想知道如何使用泛型参数重构以下代码。

第一种方法

 private Dictionary<List<string>, List<string>> GetFinancialLtmDataSet(List<sp_get_company_balance_sheet_amount_ltm_Result> itemResult, int neededyear)
    {
        var requestedData =
            itemResult.OrderByDescending(x => x.date.Year).Take(neededyear).Select(x => new { date = x.date.Date });

        var addFields = new List<string>();
        var dataSet = new Dictionary<List<string>, List<string>>();
        int counter = 0;
        foreach (var itemy in requestedData)
        {
            var skipvalue = itemResult.Skip(counter);
            var columns = skipvalue.OrderBy(x => itemy.date).ToList();
            var cc = columns.First();
            counter++;
            var properties =
                cc.GetType()
                    .GetProperties()
                    .Select(x => new { Name = x.Name, Value = x.SetMethod, a = x.GetValue(cc, null) })
                    .ToList();

            foreach (var property in properties)
            {
                addFields.Add(property.Name);
                if (property.a != null)
                {
                    dataSet.Add(new List<string> { property.Name }, new List<string> { property.a.ToString() });
                }
            }
        }
        return dataSet;
    }

第二种方法

private Dictionary<List<string>, List<string>> GetFinancialQuartelyDataSet(List<sp_get_company_balance_sheet_amount_quaterly_Result> itemResult, int neededyear)
    {
        var requestedData =
            itemResult.OrderByDescending(x => x.date.Year).Take(neededyear).Select(x => new { date = x.date.Date });

        var addFields = new List<string>();
        var dataSet = new Dictionary<List<string>, List<string>>();
        int counter = 0;
        foreach (var itemy in requestedData)
        {
            var skipvalue = itemResult.Skip(counter);
            var columns = skipvalue.OrderBy(x => itemy.date).ToList();
            var cc = columns.First();
            counter++;
            var properties =
                cc.GetType()
                    .GetProperties()
                    .Select(x => new { Name = x.Name, Value = x.SetMethod, a = x.GetValue(cc, null) })
                    .ToList();

            foreach (var property in properties)
            {
                addFields.Add(property.Name);
                if (property.a != null)
                {
                    dataSet.Add(new List<string> { property.Name }, new List<string> { property.a.ToString() });
                }
            }
        }
        return dataSet;
    }

我已经创建了一个以下方法来使其成为通用的,但是无法获得任何建议的最终实现。

private List<T> GetFinancialReport<T>(List<T> data, int neededyear)
  {

       //what should I return from here 
        return data;
  }

and would like to use the above method  like this


 var balancesheetResult=balancesheet.ToList();
 var testData = GetFinancialReport<BalanceSheet_sp>(balancesheetResult, 5);

var cashflowresult=cashflow.ToList();
var testData1 = GetFinancialReport<CahsFlow_sp>(cashflowresult, 10);

1 个答案:

答案 0 :(得分:1)

从上面显示的对象(至少涉及的属性)匹配。所以你可以在这里编写一个接口代码:

private Dictionary<List<string>, List<string>> GetFinancialReport(List<IBalance>, int neededyear) 
{
    ...
}