{"id":3509,"date":"2014-03-27T07:52:49","date_gmt":"2014-03-27T07:52:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/27\/getting-minimum-min-for-datetime-column-in-a-datatable-using-linq-to-datasets-collection-of-common-programming-errors-2\/"},"modified":"2014-03-27T07:52:49","modified_gmt":"2014-03-27T07:52:49","slug":"getting-minimum-min-for-datetime-column-in-a-datatable-using-linq-to-datasets-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/27\/getting-minimum-min-for-datetime-column-in-a-datatable-using-linq-to-datasets-collection-of-common-programming-errors-2\/","title":{"rendered":"Getting minimum &#8211; Min() &#8211; for DateTime column in a DataTable using LINQ to DataSets?-Collection of common programming errors"},"content":{"rendered":"<p>I need to get the minimum DateTime value of a column in a DataTable. The DataTable is generated dynamically from a CSV file, therefore I don&#8217;t know the name of that column until runtime. Here is code I&#8217;ve got that doesn&#8217;t work&#8230;<\/p>\n<pre><code>private DateTime GetStartDateFromCSV(string inputFile, string date_attr)\n{\n    EnumerableRowCollection table = CsvStreamReader.GetDataTableFromCSV(inputFile, \"input\", true).AsEnumerable();\n    DateTime dt = table.Select(record =&gt; record.Field(date_attr)).Min();\n    return dt;\n}\n<\/code><\/pre>\n<p>The variable table is broken out just for clarity. I basically need to find the minimum value as a <code>DateTime<\/code> for one of the columns (to be chosen at runtime and represented by <code>date_attr<\/code>).<\/p>\n<p>I have tried several solutions from SO (most deal with known columns and\/or non-DateTime fields). What I&#8217;ve got throws an error at runtime telling me that it can&#8217;t do the DateTime conversion (that seems to be a problem with Linq?)<\/p>\n<p>I&#8217;ve confirmed that the data for the column name that is in the string <code>date_attr<\/code> is a date value.<\/p>\n<p>UPDATE: Stacktrace as requested:<\/p>\n<pre><code>System.InvalidCastException was unhandled by user code\n  Message=\"Specified cast is not valid.\"\n  Source=\"System.Data.DataSetExtensions\"\n  StackTrace:\n       at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)\n       at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)\n       at Presenter.Views.NetworkVisualizationDetailPresenter.c__DisplayClass1.b__0(DataRow t) in Presenter\\Views\\NetworkVisualizationDetailPresenter.cs:line 194\n       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\n       at System.Linq.Enumerable.Min[TSource](IEnumerable`1 source)\n       at System.Linq.Enumerable.Min[TSource,TResult](IEnumerable`1 source, Func`2 selector)\n       atPresenter.Views.NetworkVisualizationDetailPresenter.GetStartDateFromCSV(String inputFile, String date_attr) in Presenter\\Views\\NetworkVisualizationDetailPresenter.cs:line 194\n       at Presenter.Views.NetworkVisualizationDetailPresenter.GetDynamicNetworkVisualizationData(String inputFile, Int32 dataMode, Int32 timeInterval, String date_attr, String entity_attr, String event_attr) in Views\\NetworkVisualizationDetailPresenter.cs:line 123\n       at Presenter.Views.NetworkVisualizationDetail.Page_Load(Object sender, EventArgs e) in NetworkVisualizationDetail.aspx.cs:line 114\n       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)\n       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)\n       at System.Web.UI.Control.OnLoad(EventArgs e)\n       at System.Web.UI.Control.LoadRecursive()\n       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)\n  InnerException: \n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I need to get the minimum DateTime value of a column in a DataTable. The DataTable is generated dynamically from a CSV file, therefore I don&#8217;t know the name of that column until runtime. Here is code I&#8217;ve got that doesn&#8217;t work&#8230; private DateTime GetStartDateFromCSV(string inputFile, string date_attr) { EnumerableRowCollection table = CsvStreamReader.GetDataTableFromCSV(inputFile, &#8220;input&#8221;, true).AsEnumerable(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3509","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3509"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3509\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}