排序日期时间字符串列在一个DataTable [英] Sort a string column by datetime in a DataTable
本文介绍了排序日期时间字符串列在一个DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想排序数据表
在字符串
按列的DateTime
。
由于种种原因,该列必须保留为字符串
数据类型。我知道我可以出的数据复制到另一个表中,列转换为的DateTime
列,排序上,并将其复制回来,但我不知道是否有一个更合适的方法
For various reasons, the column must be left as a string
data type. I know I can copy the data out into another table, convert that column to a DateTime
column, sort on that and copy it back but I'm wondering if there's a neater way.
我试过默认视图
的基础知识和 LINQ
没有占上风。
I've tried the basics of DefaultView
and LINQ
to no prevail.
推荐答案
尝试添加而创建数据表
table.Columns.Add("dateValue", typeof(DateTime?));
var orderedRows = from row in dt.AsEnumerable()
orderby row.Field<DateTime>("Date")
select row;
DataTable tblOrdered = orderedRows.CopyToDataTable();
(OR)
var orderedRows = from row in dt.AsEnumerable()
let date = DateTime.Parse(row.Field<string>("Date"), CultureInfo.InvariantCulture)
orderby date
select row;
这篇关于排序日期时间字符串列在一个DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文