Sort a string column by datetime in a DataTable

前端 未结 4 805
名媛妹妹
名媛妹妹 2021-01-15 14:45

I am trying to sort a DataTable on a string column by DateTime.

For various reasons, the column must be left as a string

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-15 14:47

    Try Add while create DataTable

        table.Columns.Add("dateValue", typeof(DateTime?));
    
        var orderedRows = from row in dt.AsEnumerable()
                          orderby  row.Field("Date")
                          select row; 
        DataTable tblOrdered = orderedRows.CopyToDataTable();
    

    (Or)

     var orderedRows = from row in dt.AsEnumerable()
                          let date = DateTime.Parse(row.Field("Date"), CultureInfo.InvariantCulture)
                          orderby date 
                          select row;
    

提交回复
热议问题