c# - Is there a way to simplify this with a loop or linq statement? -


i'm trying find out if there way create loop example code below

// objects below create 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 similar below pseudo code. please keep in mind each var item 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 list of custom class have added below. i'm trying cycle through each variable in class , perform select query perform goodness of fit rsquared calculation on list of decimals select query returns simplifies code , makes similar 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: tried local function , fails ; expected , invalid {

double r(func<calculationdata, double> f) => { goodnessoffit.rsquared(calc.listcalculationdata.select(f), vectorarray) }; 

update 2: have current code set because of recommendations doesn't work because says name doesn't exist in context @ section: nameof(i.ema12) , because i'm using 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)            }      }   } 

you can either express each individual field lambda retrieves particular field value (i think better) or string or propertytype value uses reflection achieve same thing.

    var getters = new func<calculationdata, double>[] {         (i) => (double)i.ema12,         (i) => (double)i.ema26,         (i) => (double)i.ema,         (i) => (double)i.adl,     }; 

then it's matter of getting each individual ienumerable<double> sequence , calculating rsquared value.

    var dataseries = getters.select((func) => calc.listcalculationdata.select(func));     double[] results = dataseries.select((data) => goodnessoffit.rsquared(data)).toarray(); 


comments:

this similar i'm looking have on 40 variables in class , added more information try explain i'm trying i'm trying prevent 40 lines of code similar code

the following should you're asking, using reflection.

 ienumerable<func<calculationdata, double>> getters =      typeof(calculationdata).getproperties()      .select<propertyinfo, func<calculationdata, double>>(          (propertyinfo p) => (calculationdata x) => (double)(decimal)p.getvalue(x)      ); 

edit: question edited again, , i'm no longer need indirection of getters. see https://dotnetfiddle.net/sb65dz barebones example of how i'd write code.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -