У меня есть 2 метода, которые почти одинаковы, но имеют небольшие различия, и я хочу сделать 1 Generi c Метод, чтобы сделать все это.
Метод 1
public List<DataGraficoNgxChart> GenerarDataGraficoNgxChart(List<EvolucionCartera> data)
{
List<DataGraficoNgxChart> series = new List<DataGraficoNgxChart> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.Clasificacion))
{
var sub = series.Find(s => s.Name == item.Clasificacion);
Series serie = new Series { Value = item.Monto, Name = item.Fecha };
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChart evolucionPatrimonio = new DataGraficoNgxChart
{
Name = item.Clasificacion,
Series = new List<Series> { new Series { Value = item.Monto, Name = item.Fecha } }
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
Метод 2
public List<DataGraficoNgxChartInvertida> GenerarDataGraficoNgxChart(List<Rentabilidad> data)
{
List<DataGraficoNgxChartInvertida> series = new List<DataGraficoNgxChartInvertida> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.Fecha))
{
var sub = series.Find(s => s.Name == item.Fecha);
SeriesInvertidaserie = new SeriesInvertida{ Value = item.Monto, Name = item.Clasificacion};
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChartInvertidaevolucionPatrimonio = new DataGraficoNgxChartInvertida
{
Name = item.Fecha,
SeriesInvertida= new List<SeriesInvertida> { new Series { Value = item.Monto, Name = item.Clasificacion } }
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
И мои классы:
private class DataGraficoNgxChart
{
public string Name { get; set; }
public List<Series> Series { get; set; }
}
private class DataGraficoNgxChartInvertida
{
public DateTime Name { get; set; }
public List<SeriesInvertida> Series { get; set; }
}
private class Series
{
public decimal? Value { get; set; }
public DateTime Name { get; set; }
}
private class SeriesInvertida
{
public decimal? Value { get; set; }
public string Name { get; set; }
}
До сих пор мне удавалось делать фразы Generi c:
public class DataGraficoNgxChart<N, T>
{
public N Name { get; set; }
public List<Series<T>> Series { get; set; }
}
public class Series<T>
{
public decimal? Value { get; set; }
public T Name { get; set; }
}
Но я борюсь выполнение Generi c Метод:
public List<DataGraficoNgxChart<N, T>> GenerarDataGraficoNgxChart<X, N, T>(List<X> data, Predicate<X> ejexName, b ejey ) // Trying to pass the parameters, but might be wrong
{
List<DataGraficoNgxChart<N, T>> series = new List<DataGraficoNgxChart<N, T>> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.Clasificacion)) // item properties gives me error (obviously, it doesn't have a way to know them)
{
var sub = series.Find(s => s.Name == item.Clasificacion); // Other error here
Series<T> serie = new Series<T> { Value = item.Monto, Name = item.Fecha }; // And here
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChart<N, T> evolucionPatrimonio = new DataGraficoNgxChart<N, T>
{
Name = item.Clasificacion, // And here again
Series = new List<Series<T>> { new Series<T> { Value = item.Monto, Name = item.Fecha } } // And and again...
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
Код неправильный (это WIP), но я не знаю, как вызывать свойства объектов generi c внутри для каждого.
Это мой полный класс со старыми методами и обобщенным c.
using System;
using System.Collections.Generic;
using System.Linq;
namespace GpiWebApp.Services
{
public interface INgxChartSpecification
{
List<DataGraficoNgxChart<N, T>> GenerarDataGraficoNgxChart<X, N, T>(List<X> data, Predicate<X> ejexName, b ejey);
List<DataGraficoNgxChart> GenerarDataGraficoNgxChart(List<RentabilidadClasificacion> data);
List<DataGraficoNgxChart> GenerarDataGraficoNgxChart(List<EvolucionCartera> data);
List<DataGraficoNgxChartInvertida> GenerarDataGraficoInvertidoNgxChart(List<EvolucionCartera> data);
}
public sealed class NgxChartSpecification : INgxChartSpecification
{
public NgxChartSpecification()
{
}
public List<DataGraficoNgxChart<N, T>> GenerarDataGraficoNgxChart<X, N, T>(List<X> data, Predicate<X> ejexName, b ejey) // Trying to pass the parameters, but might be wrong
{
List<DataGraficoNgxChart<N, T>> series = new List<DataGraficoNgxChart<N, T>> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.Clasificacion)) // item properties gives me error (obviously, it doesn't have a way to know them)
{
var sub = series.Find(s => s.Name == item.Clasificacion); // Other error here
Series<T> serie = new Series<T> { Value = item.Monto, Name = item.Fecha }; // And here
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChart<N, T> evolucionPatrimonio = new DataGraficoNgxChart<N, T>
{
Name = item.Clasificacion, // And here again
Series = new List<Series<T>> { new Series<T> { Value = item.Monto, Name = item.Fecha } } // And and again...
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
public List<DataGraficoNgxChart> GenerarDataGraficoNgxChart(List<RentabilidadClasificacion> data)
{
List<DataGraficoNgxChart> series = new List<DataGraficoNgxChart> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.dscArbol))
{
var sub = series.Find(s => s.Name == item.dscArbol);
Series serie = new Series { Value = item.ValorCuota, Name = item.FechaCierre };
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChart evolucionPatrimonio = new DataGraficoNgxChart
{
Name = item.dscArbol,
Series = new List<Series> { new Series { Value = item.ValorCuota, Name = item.FechaCierre } }
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
public List<DataGraficoNgxChart> GenerarDataGraficoNgxChart(List<EvolucionCartera> data)
{
List<DataGraficoNgxChart> series = new List<DataGraficoNgxChart> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.Clasificacion))
{
var sub = series.Find(s => s.Name == item.Clasificacion);
Series serie = new Series { Value = item.Monto, Name = item.Fecha };
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChart evolucionPatrimonio = new DataGraficoNgxChart
{
Name = item.Clasificacion,
Series = new List<Series> { new Series { Value = item.Monto, Name = item.Fecha } }
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
public List<DataGraficoNgxChartInvertida> GenerarDataGraficoInvertidoNgxChart(List<EvolucionCartera> data)
{
List<DataGraficoNgxChartInvertida> series = new List<DataGraficoNgxChartInvertida> { };
foreach (var item in data)
{
if (series.Exists(s => s.Name == item.Fecha))
{
var sub = series.Find(s => s.Name == item.Fecha);
SeriesInvertida serie = new SeriesInvertida { Value = item.Monto, Name = item.Clasificacion };
sub.Series.Add(serie);
}
else
{
DataGraficoNgxChartInvertida evolucionPatrimonio = new DataGraficoNgxChartInvertida
{
Name = item.Fecha,
Series = new List<SeriesInvertida> { new SeriesInvertida { Value = item.Monto, Name = item.Clasificacion } }
};
series.Add(evolucionPatrimonio);
}
}
return series.OrderBy(s => s.Name).ToList();
}
}
public class DataGraficoNgxChart<N, T>
{
public N Name { get; set; }
public List<Series<T>> Series { get; set; }
}
public class Series<T>
{
public decimal? Value { get; set; }
public T Name { get; set; }
}
public class DataGraficoNgxChart
{
public string Name { get; set; }
public List<Series> Series { get; set; }
}
public class Series
{
public decimal? Value { get; set; }
public DateTime Name { get; set; }
}
public class DataGraficoNgxChartInvertida
{
public DateTime Name { get; set; }
public List<SeriesInvertida> Series { get; set; }
}
public class SeriesInvertida
{
public decimal? Value { get; set; }
public string Name { get; set; }
}
public class RentabilidadClasificacion
{
public DateTime FechaCierre { get; set; }
public decimal IdArbol { get; set; }
public string dscArbol { get; set; }
public decimal Patrimonio { get; set; }
public decimal Aporet { get; set; }
public decimal Utilidad { get; set; }
public decimal Rentabilidad { get; set; }
public decimal ValorCuota { get; set; }
public decimal Distribucion { get; set; }
public decimal Nivel { get; set; }
}
public class EvolucionCartera
{
public DateTime Fecha { get; set; }
public string Clasificacion { get; set; }
public decimal? Monto { get; set; }
public decimal NivelArbol { get; set; }
}
}
Есть какие-нибудь идеи для помощи?