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

本文关键字:重构 参数 方法 泛型 linq | 更新日期: 2023-09-27 18:00:24

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

第一种方法

 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);

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

根据上面显示的内容,对象(至少涉及的属性)匹配。因此,您可以针对以下接口进行编码:

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