C# 数据处理逻辑帮助

发布于 2024-09-19 02:29:02 字数 606 浏览 3 评论 0原文

我有一个像这样的数据表,

AccessDateTime         | Direction
2010-09-15 12:12:49 | IN
2010-09-15 12:36:03 | OUT
2010-09-15 12:53:05 | IN
2010-09-15 14:04:19 | OUT
2010-09-15 14:17:35 | IN
2010-09-15 16:07:57 | OUT
2010-09-15 16:10:57 | OUT
2010-09-15 18:43:18 | OUT

我需要一个快速逻辑将数据转换为这种格式

Date       | In Time | Out Time
2010-09-15  12:12:49 | 12:36:03
2010-09-15  12:53:05 | 14:04:19
2010-09-15  14:17:35 | 16:07:57
2010-09-15  N/A      | 16:07:57
2010-09-15  N/A      | 16:10:57
2010-09-15  N/A      | 18:43:18

请帮助我找到任何示例代码或任何建议,非常感谢。

谢谢

I have a DataTable like this,

AccessDateTime         | Direction
2010-09-15 12:12:49 | IN
2010-09-15 12:36:03 | OUT
2010-09-15 12:53:05 | IN
2010-09-15 14:04:19 | OUT
2010-09-15 14:17:35 | IN
2010-09-15 16:07:57 | OUT
2010-09-15 16:10:57 | OUT
2010-09-15 18:43:18 | OUT

I need a fast logic to convert the data into this format

Date       | In Time | Out Time
2010-09-15  12:12:49 | 12:36:03
2010-09-15  12:53:05 | 14:04:19
2010-09-15  14:17:35 | 16:07:57
2010-09-15  N/A      | 16:07:57
2010-09-15  N/A      | 16:10:57
2010-09-15  N/A      | 18:43:18

Please help me to find any sample code or any advice is highly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-09-26 02:29:02
DataTable output = new DataTable();

using (var e = dataTable.AsEnumerable().GetEnumerator())
{
    if (!e.MoveNext())
        // No data in the datatable at all
        return;

    // Get first row
    var dt = (DateTime) e.Current["AccessDateTime"];
    var row = ((Direction) e.Current["Direction"] == Direction.In)
        ? new { Date = dt.Date, InTime = (TimeSpan?) dt.TimeOfDay, OutTime = (TimeSpan?) null }
        : new { Date = dt.Date, InTime = (TimeSpan?) null, OutTime = (TimeSpan?) dt.TimeOfDay };
    DataRow newRow;

    // Look at all the other rows
    while (e.MoveNext())
    {
        dt = (DateTime) e.Current["AccessDateTime"];
        if ((Direction) e.Current["Direction"] == Direction.Out && row.OutTime == null)
        {
            row = new { Date = row.Date, InTime = row.InTime, OutTime = (TimeSpan?) dt.TimeOfDay };
            continue;
        }

        newRow = output.NewRow();
        newRow["Date"] = row.Date;
        newRow["InTime"] = row.InTime;
        newRow["OutTime"] = row.OutTime;
        row = new { Date = dt.Date, InTime = (TimeSpan?) dt.TimeOfDay, OutTime = (TimeSpan?) null };
    }

    newRow = output.NewRow();
    newRow["Date"] = row.Date;
    newRow["InTime"] = row.InTime;
    newRow["OutTime"] = row.OutTime;
}
DataTable output = new DataTable();

using (var e = dataTable.AsEnumerable().GetEnumerator())
{
    if (!e.MoveNext())
        // No data in the datatable at all
        return;

    // Get first row
    var dt = (DateTime) e.Current["AccessDateTime"];
    var row = ((Direction) e.Current["Direction"] == Direction.In)
        ? new { Date = dt.Date, InTime = (TimeSpan?) dt.TimeOfDay, OutTime = (TimeSpan?) null }
        : new { Date = dt.Date, InTime = (TimeSpan?) null, OutTime = (TimeSpan?) dt.TimeOfDay };
    DataRow newRow;

    // Look at all the other rows
    while (e.MoveNext())
    {
        dt = (DateTime) e.Current["AccessDateTime"];
        if ((Direction) e.Current["Direction"] == Direction.Out && row.OutTime == null)
        {
            row = new { Date = row.Date, InTime = row.InTime, OutTime = (TimeSpan?) dt.TimeOfDay };
            continue;
        }

        newRow = output.NewRow();
        newRow["Date"] = row.Date;
        newRow["InTime"] = row.InTime;
        newRow["OutTime"] = row.OutTime;
        row = new { Date = dt.Date, InTime = (TimeSpan?) dt.TimeOfDay, OutTime = (TimeSpan?) null };
    }

    newRow = output.NewRow();
    newRow["Date"] = row.Date;
    newRow["InTime"] = row.InTime;
    newRow["OutTime"] = row.OutTime;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文