使用 DataRowExtension 解析 TimeSpan

我想使用 DataRowExtension 将 DataRow 中的字段值作为 TimeSpan(格式如 mm:ss),但它给了我 System.InvalidCastException,如下所示


var time = staffItems.Rows[0].Field<TimeSpan>("TIME_DURATION"); // System.InvalidCastException

但是当将此值作为字符串并在 Parse to TimeSpan 之后没有问题发生。


var time = staffItems.Rows[0].Field<string>("TIME_DURATION"); // time : 0:43

var time2 = TimeSpan.Parse(time); // time2 : 00:43:00

问题是,我如何在没有任何额外解析或强制转换的情况下使用 DataRowExtension 来做到这一点。


慕妹3146593
浏览 166回答 3
3回答

料青山看我应如是

可能 TIME_DURATION 字段来自 DataTable 的 vharchar 或其他内容。它必须等同于 TimeSpan。

蛊毒传说

首先你必须解析然后获取时间部分var time = TimeSpan.Parse(staffItems.Rows[0]["TIME_DURATION"]);var time2 = time.ToString(@"mm\:ss");如果你想从 datarowextension 获得它。您必须创建数据表并为“TIME_DURATION”列指定时间对象。您可以通过以下方法做到这一点:using System;using System.Data;class Program{&nbsp; &nbsp; static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; // Loop over DataTable rows and call the Field extension method.&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; foreach (DataRow row in GetTable().Rows)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get first field by column index.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int weight = row.Field<int>(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get second field by column name.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string name = row.Field<string>("Name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get third field by column index.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string code = row.Field<string>(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get fourth field by column name.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime date = row.Field<DateTime>("Date");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Display the fields.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("{0} {1} {2} {3}", weight, name, code, date);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static DataTable GetTable()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DataTable table = new DataTable(); // Create DataTable&nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Weight", typeof(int)); // Add four columns&nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Name", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Code", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; table.Columns.Add("Date", typeof(DateTime));&nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(57, "Koko", "A", DateTime.Now); // Add five rows&nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(130, "Fido", "B", DateTime.Now);&nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(92, "Alex", "C", DateTime.Now);&nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(25, "Charles", "D", DateTime.Now);&nbsp; &nbsp; &nbsp; &nbsp; table.Rows.Add(7, "Candy", "E", DateTime.Now);&nbsp; &nbsp; &nbsp; &nbsp; return table;&nbsp; &nbsp; }}

慕盖茨4494581

下面的方法将 a 解析string为扩展方法TimeSpan中的 a 。DataRowpublic static TimeSpan ExtractTimeData(this DataRow row, string column){&nbsp; &nbsp; // check column exists in dataTable&nbsp; &nbsp; var exists = row.Table.Columns.Contains(column);&nbsp; &nbsp; if (exists)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // ensure we're not trying to parse null value&nbsp; &nbsp; &nbsp; &nbsp; if (row[column] != DBNull.Value)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimeSpan time;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (TimeSpan.TryParse(row[column].ToString(), out time))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return if we can parse to TimeSpan&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return time;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // return default TimeSpan if there is an error&nbsp; &nbsp; return default(TimeSpan);}你可以像这样使用它:TimeSpan time = row.ExtractTimeData("TIME_DURATION");string timeString = time.ToString(@"h\:mm");
打开App,查看更多内容
随时随地看视频慕课网APP