Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring LINQ methods
    primarykey
    data
    text
    <p>I have a function that takes a <code>DataTable</code> as a parameter and returns an object of type <code>NormalData</code> for each column in the data table NormalData definition</p> <pre><code>public class NormalData { //AttributeName = ColumnName of DataTable public string AttributeName { get; set; } //each column will have its mean and standard deviation computed public double Mean { get; set; } public double StandardDeviation { get; set; } //a DataTable with three columns will create an IEnumerable&lt;NormalData&gt; //with a count of three } </code></pre> <p>The following works, but I would like a second opinion on how I implemented it:</p> <pre><code>public static IEnumerable&lt;NormalData&gt; GetNormalDataByTableColumns(DataTable dt) { //get list of column names to iterate over List&lt;string&gt; columnList = GetDataTableColumnNames(dt); List&lt;NormalData&gt; normalDataList = new List&lt;NormalData&gt;(); for (int i = 0; i &lt; columnList.Count; i++) { //creates a NormalData object for each column in the DataTable NormalData normalData = new NormalData(); //find average normalData.Mean = GetColumnAverage(dt, columnList[i]); //find stDev normalData.StandardDeviation = GetColumnStDev(dt,columnList[i],normalData.Mean); normalData.AttributeName = columnList[i]; //add to NormalDataList normalDataList.Add(normalData); } return normalDataList; } private static List&lt;string&gt; GetDataTableColumnNames(DataTable dt) { return (from DataColumn dc in dt.Columns select dc.ColumnName).ToList(); } private static double GetColumnAverage(DataTable dt, string columnName) { return dt.AsEnumerable().Average(x =&gt; x.Field&lt;double&gt;(columnName)); } private static double GetColumnStDev(DataTable dt, string columnName,double average) { var squaredDiffs = (dt.AsEnumerable() .Sum(x =&gt; (x.Field&lt;double&gt;(columnName) - average) * x.Field&lt;double&gt;(columnName) - average)); return Math.Sqrt(squaredDiffs / dt.Rows.Count); } </code></pre> <p>What I feel is poor design is the parameter list that <code>GetColumnAverage</code> and <code>GetColumnStDev</code> are required to take. In reality, they should only need a list of numeric types (not necessarily double, but it's hardcoded at the moment) to compute their values. This is, however, the only way I've gotten this to work this morning. What are some rules that I'm breaking here in this design? How can I amend this so that the <code>GetColumn..</code> functions only take the <code>DataColumn</code> that's being iterated over in the <code>for</code> loop of <code>columnList</code>?</p> <p>EDIT: the <code>average</code> variable is changed for each column and cannot be reused. Or is it possible that this is OK design and I need to have overloaded versions of these methods if I don't need to compute the standard deviation and yes, only the average?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload