I'm trying to find out if there is a way to create a loop for my example code below
// the objects below create a list of decimals
var ema12 = calc.ListCalculationData.Select(i => (double)i.Ema12);
var ema26 = calc.ListCalculationData.Select(i => (double)i.Ema26);
var ema = calc.ListCalculationData.Select(i => (double)i.Ema);
var adl = calc.ListCalculationData.Select(i => (double)i.ADL);
var r1 = GoodnessOfFit.RSquared(ema12);
var r2 = GoodnessOfFit.RSquared(ema26);
var r3 = GoodnessOfFit.RSquared(ema);
var r4 = GoodnessOfFit.RSquared(adl);
I'm trying to get something similar to the below pseudo code. Please keep in mind that each var item is a list of decimals
foreach (var item in calc.ListCalculationData.AsEnumerable())
{
var item2 = calc.ListCalculationData.Select(i => (double)item);
var r1 = GoodnessOfFit.RSquared(item2);
}
More information:
ListCalculationData is a list of my custom class that I have added below. What I'm trying to do is cycle through each variable in that class and perform a select query to perform the goodness of fit rsquared calculation on the list of decimals that the select query returns so it simplifies my code and makes it similar to my pseudo code
public class CalculationData
{
public decimal Ema { get; set; }
public decimal Ema12 { get; set; }
public decimal Ema26 { get; set; }
public decimal ADL { get; set; }
}
Update: I tried this for a local function and it fails with ; expected and invalid {
double r(Func<CalculationData, double> f) =>
{ GoodnessOfFit.RSquared(calc.ListCalculationData.Select(f), vectorArray) };
Update 2: This is what I have my current code set to because of the recommendations but obviously this doesn't work because it says that the name i doesn't exist in this context at this section: nameof(i.Ema12) and also because I'm using mostly pseudo code
MultipleRegressionInfo rn(Func<CalculationData, double> f, string name, int days)
{
MultipleRegressionInfo mrInfo = new MultipleRegressionInfo
{
RSquaredValue = GoodnessOfFit.RSquared(calc.ListCalculationData.Select(f), vectorArray),
ListValues = (List<double>)calc.ListCalculationData.Select(f).ToList(),
ValueName = name,
Days = days
};
listMRInfo.Add(mrInfo);
return mrInfo;
};
MultipleRegressionInfo rnList(Func<CalculationData, List<decimal>> f, string name, int days)
{
MultipleRegressionInfo mrInfo = new MultipleRegressionInfo
{
RSquaredValue = GoodnessOfFit.RSquared(calc.ListCalculationData.Select(f), vectorArray),
ListValues = (List<double>)calc.ListCalculationData.Select(f).ToList(),
ValueName = name,
Days = days
};
listMRInfo.Add(mrInfo);
return mrInfo;
};
foreach (CalculationData calc in ListCalculationData)
{
foreach (object value in calc)
{
if (value == typeof(decimal))
{
MultipleRegressionInfo r1 = rn(i => (double)i.value, nameof(i.value), 100)
}
else if (value == typeof(List<decimal>)
{
MultipleRegressionInfo r1 = rnList(i => i.value, nameof(i.value), 100)
}
}
}